JavaScript Strings

JavaScript strings are used to store and manipulate text.

JavaScript Strings

JavaScript strings are zero or more characters enclosed in quotes.

Example

var x = \"Bill Gates\";

Try It Yourself

You can use either single quotes or double quotes:

Example

var carname = \"Porsche 911\";
var carname = 'Porsche 911';

Try It Yourself

You can use quotes in strings, as long as they do not match the quotes surrounding the string:

Example

var answer = \"It's good to see you again!\";
var answer = \"He is called 'Bill'\";
var answer = 'He is called \"Bill\"';

Try It Yourself

String length

Built-in property length Can return theLength:

Example

var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;

Try It Yourself

Special characters

Since strings must be enclosed in quotes, JavaScript will misinterpret this string:

var y = "China is the hometown of porcelain, so china is the same as"China (China)".";

The string will be split into "China is the hometown of porcelain, so china is the same as".

The solution to avoid this problem is to use \ Escape character.

The backslash escape character converts special characters into string characters:

Code Result Description
\' ' Single quote
\" " Double quotes
\\ \ Backslash

Example

Sequence \" Inserting double quotes into a string:

Example

var x = "China is the hometown of porcelain, so china is the same name as \"China (China)\"."

Try It Yourself

Sequence \' Inserting a single quote into a string:

Example

var x = 'It\'s good to see you again';

Try It Yourself

Sequence \\ Inserting a backslash into a string:

Example

var x = "The character \\ is called a backslash.";

Try It Yourself

Escape characters (\) can also be used to insert other special characters into strings.

Other six valid escape sequences in JavaScript:

Code Result
\b Backspace key
\f Page break
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab

These six escape characters were originally designed to control typewriters, teletype machines, and fax machines. They have no meaning in HTML.

Breaking long code lines

For the best readability, programmers usually avoid having more than 80 characters per line of code.

If a JavaScript statement does not fit on a single line, the best line break position is after an operator:

Example

document.getElementById("demo").innerHTML =
"Hello Kitty.";

Try It Yourself

You can alsoIn stringsBreaking lines in strings, just by a backslash:

Example

document.getElementById("demo").innerHTML = "Hello \
Kitty!";

Try It Yourself

\ This method is not part of the ECMAScript (JavaScript) standard.

Some browsers also do not allow \ There is a space after the character.

The safest way to break long strings (but it can be slow) is to use string concatenation:

Example

document.getElementById("demo").innerHTML = "Hello" + 
"Kitty!";

Try It Yourself

You cannot break code lines with backslashes:

Example

document.getElementById("demo").innerHTML = \ 
"Hello Kitty!";

Try It Yourself

Strings can also be objects

Typically, JavaScript strings are primitive values, created by literal notation:

var firstName = "Bill"

But strings can also be declared using the keyword new Defined as an object:

var firstName = new String("Bill")

Example

var x = "Bill";
var y = new String("Bill");
// typeof x will return string
// typeof y will return object

Try It Yourself

Please do not create strings as objects. It will slow down the execution speed.

new keyword can complicate the code. It may also produce some unexpected results:

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

Example

var x = "Bill";             
var y = new String("Bill");
// (x == y) is true because x and y have the same value

Try It Yourself

When using === operator, the equal strings are not equal because === The operator needs both type and value to be equal.

Example

var x = "Bill";             
var y = new String("Bill");
// (x === y) is false because x and y are of different types (string and object)

Try It Yourself

Even worse. Objects cannot be compared:

Example

var x = new String("Bill");             
var y = new String("Bill");
// (x == y) is false because x and y are different objects

Try It Yourself

Example

var x = new String("Bill");             
var y = new String("Bill");
// (x === y) is false because x and y are different objects

Try It Yourself

Please note the difference between (x==y) and (x===y).

JavaScript objects cannot be compared, comparing two JavaScript objects will always return false.