JavaScript Strings

String Declaration


<!DOCTYPE html>
<html>
<body>
    <h1>JavaScript Strings</h1>
    <script>
        let str1 = "Hello, AIT!";  
        let str2 = 'JavaScript Strings!';
        console.log(str1); // Output: Hello, AIT!
        console.log(str2);
    </script>
</body>
</html>
                                                                    

Output:


Output: JavaScript Strings!

String Methods

1. Length


<!DOCTYPE html>
<html>
<body>
    <h1>String Method</h1>
    <h2>Lenght</h2>
    <script>
        let str = "Hello, AIT!";  
        console.log(str.length);
    </script>
</body>
</html>                                

Output:


Output: 11

2. toUpperCase() and toLowerCase()


<!DOCTYPE html>
<html>
    <body>
    <h2>toUpperCase() and toLowerCase()</h2>
    <script>
        let str = "AIT Aurangabad";  
        console.log(str.toUpperCase()); // Output: Hello, AIT!
        let str2 = "AIT Aurangabad";
        console.log(str2.toLowerCase());
    </script>
</body>
</html>  

Output:

let str = "AIT Aurangabad";
Output: "AIT AURANGABAD"
Output: "ait aurangabad"

3. charAt()


<!DOCTYPE html>
<html>
<body>
    <h2>charAt()</h2>
    <script>
        let str = "Hello"; 
        console.log(str.charAt(1));
    </script>
</body>
</html>    

Output:

                                                              
 Output: "e"

4. indexOf()


<!DOCTYPE html>
<html>
<body>
    <h2>indexOf()</h2>
    <script>
        let str = "Hello, world!";
        console.log(str.indexOf("world")); // Output: 7
        console.log(str.indexOf("JavaScript"));
    </script>
</body>
</html>    

Output:

                                 
Output: 7
Output: -1 (not found)

5. substring()


<!DOCTYPE html>
<html>
<body>
    <h2>substring()</h2>
    <script>
        let str = "JavaScript String";
        console.log(str.substring(0, 10)); 
    </script>
</body>
</html>    

Output:

                                 
Output: "JavaScript"

6. split()


<!DOCTYPE html>
<html>
<body>
    <h2>split()</h2>
    <script>
        let str = "Hello World";
        let arr = str.split(" ");
        console.log(arr);
    </script>
</body>
</html>    

Output:

                                   
Output: ["Hello", "World"]

7. replace()


<!DOCTYPE html>
<html>
<body>
    <h2>replace()</h2>
    <script>
        let str = "Hello, World!";
        let newStr = str.replace("World", "JavaScript");
        console.log(newStr);
    </script>
</body>
</html>    

Output:

                                 
Output: "Hello, JavaScript!"

8. trim()


<!DOCTYPE html>
<html>
<body>
    <h2>trim()</h2>
    <script>                               
        let str = "   Hello, World!   ";
        console.log(str.trim());
    </script>
</body>
</html>    

Output:

                                 
Output: "Hello, World!"

9. concat()


<!DOCTYPE html>
<html>
<body>
    <h2>concat()</h2>
    <script>
        let str1 = "Hello";
        let str2 = "World";
        console.log(str1.concat(", ", str2, "!"));
    </script>
</body>
</html>    

Output:

                                   
Output: "Hello, World!"

10. Template Literals


<!DOCTYPE html>
<html>
<body>
    <h2>Template Literals</h2>
    <script>
        let name = "Noorain";
        let greeting = Hello, ${name}!;
        console.log(greeting);
    </script>
</body>
</html>    

Output:

                                  
Output: "Hello, Noorain!"