JavaScript Strings


JavaScript strings are used to store and handle text.


JavaScript Strings

A string can store a series of characters, such as "John Doe".

A string can be any character enclosed in quotes. You can use single or double quotes:

Example

var carname = "Volvo XC60";
var carname = 'Volvo XC60';

You can use index positions to access each character in a string:

Example

var character = carname[7];

String indexes start from 0, meaning the index value of the first character is[0], the second is[1], and so on.

Example

const name = "EXAMPLE";
let letter = name[2];

document.getElementById("demo").innerHTML = letter;

Try it Yourself »

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

Example

var answer = "It's alright";
var answer = "He is called 'Johnny'";
var answer = 'He is called "Johnny"';

You can also use escape characters to add quotes within a string:

Example

var x = 'It\'s alright';
var y = "He is called \"Johnny\"";

Try it Yourself »


String Length

The built-in propertylengthcan be used to calculate the length of a string:

Example

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

Try it Yourself »


Special Characters

In JavaScript, strings are written inside single or double quotes.

Because of this, JavaScript cannot parse the following example:

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

The string "We are the so-called " is truncated.

How to solve the problem above? You can use a backslash (\) to escape the double quotes in the "Vikings" string, as follows:

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

The backslash is anescape character. The escape character converts special characters into string characters:

The escape character (\) can be used to escape apostrophes, newlines, quotes, and other special characters.

The table below lists the special characters that can be escaped using escape characters in strings:

Code Output
\' Single quote
\" Double quote
\\ Backslash
\n Newline
\r Carriage return
\t Tab
\b Backspace
\f Form feed


Strings Can Be Objects

Normally, JavaScript strings are primitive values, created using literals:var firstName = "John"

But we can also define a string as an object using the new keyword:var firstName = new String("John")

Example

var x = "John";
var y = new String("John");
typeof x // returns String
typeof y // returns Object

Try it Yourself »

Note Don't create String objects. It slows down execution speed and may produce other side effects:

Example

var x = "John";             
var y = new String("John");
(x === y) // result is false, because x is a string and y is an object

Try it Yourself »

=== means absolute equality, i.e., both the data type and the value must be equal.



String Properties and Methods

Primitive value strings, such as "John", have no properties or methods (because they are not objects).

Primitive values can use JavaScript's properties and methods, because JavaScript can treat primitive values as objects when executing methods and properties.

We will introduce string methods in the next chapter.


String Properties

Property Description
constructor Returns the function that created the string property
length Returns the length of the string
prototype Allows you to add properties and methods to objects


String Methods

For more method examples, see:JavaScript String Object。

Method Description
charAt() Returns the character at the specified index position
charCodeAt() Returns the Unicode value of the character at the specified index position
concat() Connects two or more strings and returns the connected string
fromCharCode() Converts Unicode to a string
indexOf() Returns the position of the first occurrence of the specified character in the string
lastIndexOf() Returns the position of the last occurrence of the specified character in the string
localeCompare() Compares two strings in a locale-specific order
match() Finds one or more matches of a regular expression
replace() Replaces substrings that match a regular expression
search() Retrieves the value that matches a regular expression
slice() Extracts a section of a string and returns the extracted part in a new string
split() Splits a string into an array of substrings
substr() Extracts a specified number of characters from a starting index in the string
substring() Extracts characters between two specified indexes in a string
toLocaleLowerCase() Converts a string to lowercase according to the host's locale; only a few languages (such as Turkish) have locale-specific case mappings
toLocaleUpperCase() Converts a string to uppercase according to the host's locale; only a few languages (such as Turkish) have locale-specific case mappings
toLowerCase() Converts a string to lowercase
toString() Returns the string object value
toUpperCase() Converts a string to uppercase
trim() Removes whitespace from both ends of a string
valueOf() Returns the primitive value of a string object
Other Extensions