This site is mobile accessible. Press the "Tap Here" button to use a different font-size.


Smartphone icons created by Freepik - Flaticon

  • 4.1 JS Strings

    • JavaScript strings are used for storing and manipulating text.
    • A JavaScript string is zero or more characters written inside quotes.
    Example 1: JS Strings part 1
    var x = "John Doe";

    You can use single or double quotes:

    Example 2: JS Strings part 2
    var carName1 = "Volvo XC60";  // Double quotes
    var carName2 = 'Volvo XC60';  // Single quotes

    You can use quotes inside a string, as long as they don't match the quotes surrounding the string:

    Example 3: JS Strings part 3
    var answer1 = "It's alright";
    var answer2 = "He is called 'Johnny'";
    var answer3 = 'He is called "Johnny"';

    String Length

    To find the length of a string, use the built-in length property:

    Example 4: JS Strings length Property
    var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var sln = txt.length;

    Escape Character

    Because strings must be written within quotes, JavaScript will misunderstand this string:

    var x = "We are the so-called "Vikings" from the north.";
    • The string will be chopped to "We are the so-called ".
    • The solution to avoid this problem, is to use the backslash escape character.
    • The backslash (\) escape character turns special characters into string characters:
    Code Result Description
    \' ' Single quote
    \" " Double quote
    \\ \ Backslash

    The sequence \" inserts a double quote in a string:

    Example 5: JS Strings the escape sequence \"
    var x = "We are the so-called \"Vikings\" from the north.";

    The sequence \' inserts a single quote in a string:

    Example 6: JS Strings the escape sequence \'
    var x = 'It\'s alright.';

    The sequence \\ inserts a backslash in a string:

    Example 7: JS Strings the escape sequence \\
    var x = "The character \\ is called backslash.";

    Six other escape sequences are valid in JavaScript:

    Code Result
    \b Backspace
    \f Form Feed
    \n New Line
    \r Carriage Return
    \t Horizontal Tabulator
    \v Vertical Tabulator

    The 6 escape characters above were originally designed to control typewriters, teletypes, and fax machines. They do not make any sense in HTML.

    Breaking Long Code Lines

    • For best readability, programmers often like to avoid code lines longer than 80 characters.
    • If a JavaScript statement does not fit on one line, the best place to break it is after an operator
    Example 8: JS Statements
    document.getElementById("demo").innerHTML =
    "Hello Dolly!";

    You can also break up a code line within a text string with a single backslash:

    Example 9: JS Strings line break
    document.getElementById("demo").innerHTML = "Hello \
    Dolly!";

    The \ method is not the preferred method. It might not have universal support. Some browsers do not allow spaces behind the \ character.

    A safer way to break up a string, is to use string addition:

    Example 10: JS Strings line break +
    document.getElementById("demo").innerHTML = "Hello " +
    "Dolly!";

    You cannot break up a code line with a backslash:

    Example 11: JS Strings line break NOT
    document.getElementById("demo").innerHTML = \
    "Hello Dolly!";

    Strings Can be Objects

    • Normally, JavaScript strings are primitive values, created from literals:
    • var firstName = "John";
    • But strings can also be defined as objects with the keyword new:
    • var firstName = new String("John");
    Example 12: JS Strings typeof
    var x = "John";
    var y = new String("John");
    
    // typeof x will return string
    // typeof y will return object

    Don't create strings as objects. It slows down execution speed. The new keyword complicates the code. This can produce some unexpected results:

    When using the == operator, equal strings are equal:

    Example 13: JS Strings - equal value
    var x = "John";             
    var y = new String("John");
    
    // (x == y) is true because x and y have equal values

    When using the === operator, equal strings are not equal, because the === operator expects equality in both type and value.

    Example 14: JS Strings - not equal type
    var x = "John";             
    var y = new String("John");
    
    // (x === y) is false because x and y have different types (string and object)

    Or even worse. Objects cannot be compared:

    Example 15: JS Strings - cannot compare Objects
    var x = new String("John");             
    var y = new String("John");
    
    // (x == y) is false because x and y are different objects
    Example 16: JS Strings - Objects cannot be compared
    var x = new String("John");             
    var y = new String("John");
    
    // (x === y) is false because x and y are different objects
    Navigate this module

    Eventually the navigation links, above, will be replaced by these << (previous) and >> (next) buttons below.



    JavaScript icons used in the buttons provided by ICONS8.COM. Smartphone icons created by Freepik - Flaticon