JavaScript - Strings

Strings are for storing text. Strings are written with quotes.

Using Quotes :
A JavaScript string is zero or more characters written inside quotes.
    < !DOCTYPE html>
    < html>
    < body>
    < h1>JavaScript Strings< /h1>
    < p id="demo">< /p>
    < p id="demo1">< /p>
    < p id="demo2">< /p>

    < script>
    let text = "Santhosh Kumar";  // String written inside double quotes
    document.getElementById("demo").innerHTML = text;

    let text1 = 'Prasanth';  // String written inside single quotes
    document.getElementById("demo1").innerHTML = text1;

    document.getElementById("demo2").innerHTML = text + " is friend of  " + text1; 
    < /script>

    < /body>
    < /html>
Output :

JavaScript Strings

Santhosh Kumar

Prasanth



String Length :
To find the length of a string, use the built-in length property
    < !DOCTYPE html>
    < html>
    < body>
    < h1>JavaScript Strings< /h1>
    < h2>The length Property< /h2>

    < p>The length of the string is:< /p>
    < p id="demo">< /p>

    < script>
        let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        document.getElementById("demo").innerHTML = text.length;
    < /script>

    < /body>
    < /html>
Output :

JavaScript Strings

The length Property

The length of the string is:

26

Escape Characters :
Because strings must be written within quotes, JavaScript will misunderstand this string:
    < !DOCTYPE html>
    < html>
    < body>

    < h1>JavaScript Strings< /h1>
    < p>The escape sequence \" inserts a double quote in a string.< /p>

    < p id="demo">< /p>

    < script>
        let text = "We are the so-called \"Vikings\" from the north.";
        document.getElementById("demo").innerHTML = text; 
    < /script>

    < /body>
    < /html>
Output :

JavaScript Strings

The escape sequence \" inserts a double quote in a string.

We are the so-called "Vikings" from the north.



We can use Strings as Objects in JS :
    < !DOCTYPE html>
    < html>
    < body>

    < h1>JavaScript Strings< /h1>
    < p id="demo">< /p>
    < p id="demo1">< /p>

    < script>
        // x is a string
        let x = "Santhosh";

        // y is an object
        let y = new String("Kumar");

        document.getElementById("demo").innerHTML = x + "< br>" +  y;
        document.getElementById("demo1").innerHTML = typeof x + "< br>" + typeof y;
    < /script>

    < /body>
    < /html>

Output :

JavaScript Strings

Santhosh

Kumar


string

object


String Methods :

Javascript strings are primitive and immutable: All string methods produce a new string without altering the original string.
    String length
    String charAt()
    String charCodeAt()
    String at()
    String [ ]
    String slice()
    String substring()
    String substr()
    String toUpperCase()
    String toLowerCase()
    String concat()
    String trim()
    String trimStart()
    String trimEnd()
    String padStart()
    String padEnd()
    String repeat()
    String replace()
    String replaceAll()
    String split()

    See Also:
    String Search Methods
    String Templates
Eg for String methods:
    < !DOCTYPE html>
    < html>
    < body>

    < h1>JavaScript Strings< /h1>
    < h2>The length & Char Property< /h2>

    < p>The length & char of the string is:< /p>
    < p id="demo">< /p>

    < h2>String at property< /h2>
    < p id="demo1">< /p>

    < h2>Accessing property< /h2>
    < p id="demo2">< /p>

    < script>
        //Length & char property
        let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        let char = text.charAt(0);
        document.getElementById("demo").innerHTML = "Length of text : " +  text.length + "< br> "  +
                                                    "Character of text : "+ text.charAt(0);
                                                    
        //String at property
        let letter = text[2];
        document.getElementById("demo1").innerHTML = letter;

        //Access property
        let access = "HELLO WORLD";
        document.getElementById("demo2").innerHTML = access[4];
    < /script>

    < /body>
    < /html>

Output :

JavaScript Strings

The length & Char Property

The length & char of the string is:

Length of text : 26

Character of text : A


String at property

C

Accessing property

O


Extracting String Parts :
There are 3 methods for extracting a part of a string:
 
    slice(start, end)       -   This method takes 2 parameters: start position, and end position (end not included)
    substring(start, end)   -   The difference is that start and end values less than 0 are treated as 0 in substring().
    substr(start, length)   -   If you omit the second parameter, substr() will slice out the rest of the string.
    toUpperCase()           -   A string is converted to upper case with toUpperCase()
    toLowerCase()           -   A string is converted to lower case with toLowerCase()
    concat()                -   concat() joins two or more strings
    trim()                  -   The trim() method removes whitespace from both sides of a string ( trimStart(),trimEnd() )

String Search Methods :
    String Search Methods
    String indexOf()
    String lastIndexOf()
    String search()
    String match()
    String matchAll()
    String includes()
    String startsWith()
    String endsWith()

    Note : These methods are inbuilt you can use with String whatever you give.


(JS - Scope)