JavaScript Tutorial
JS - Introduction JS - Basic JS - Placement JS - Output JS - Statements JS - Syntax JS - Variables JS - Comments JS - Data Types JS - Operators JS - Operator Precedence JS - Condition JS - Switch JS - Functions JS - Objects JS - Loops For JS - Loops While JS - Break JS - Strings JS - String Methods JS - Numbers JS - Number Methods JS - Arrays JS - Array Methods JS - Array Iteration JS - Events JS - Event Listener JS - Event Propagation JS - Date JS - Math JS - Random JS - Boolean JS - Type Conversion JS - RegExp JS - Exception JS - Scope JS - Debugging JS - Hoisting JS - Strict Mode JS - this Keyword JS - Mistakes JS - Best Practices JS - Performance JS - Form Validation JS - ES6 Features
JS Objects
Object Definitions Object Properties Object Methods Object Constructors Object Prototypes
JS Functions
Function Definitions Function Parameters Function Call Function Apply Function Closures
JS HTML DOM
DOM Introduction DOM Methods DOM Selectors DOM HTML DOM CSS DOM Attributes DOM Navigation
JS Browser BOM
JS - Window JS - Screen JS - Location JS - History JS - Navigator JS - Popup Alert JS - Timing JS - Cookies
JS AJAX
AJAX - Introduction AJAX - XMLHttp AJAX - Request AJAX - Response AJAX - PHP
JS JSON
JSON - Introduction JSON - Syntax JSON - Data Types JSON - Parse JSON - Stringify JSON - Objects JSON - Arrays JSON - PHP JSON - JSONP
JS References
JavaScript Reference JavaScript Methods HTML DOM Reference

JavaScript Strings

A string is a sequence of one or more characters that may consist of letters, numbers, or symbols.

Strings in JavaScript are primitive data types and immutable, which means they are unchanging.


JavaScript Strings

A JavaScript string is zero or more characters written inside quotes.

Run code

In JavaScript, you can choose single quotes or double quotes to wrap your strings in. Both of the following will work okay:

Run code

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

Run code

The newest way to create a string is called a template literal.

Template literals use the backtick (` `) and work the same way as regular strings:

Run code

String Length

The length property returns the length of a string. For an empty string, length is 0.

Run code

Note: A White-space is also counted as character.


String Concatenation

Concatenation means joining two or more strings together to create a new string.

The + operator is used to add (concatenate) strings.

Run code

One special feature of the template literal feature is the ability to include expressions and variables within a string. Instead of having to use concatenation, we can use the ${} syntax to insert a variable.

Run code

In this case, using template literals might be easier to write and more convenient.



Escape Sequences

Because strings must be written within quotes, the following will error, as it confuses the browser as to where the string ends:

Escaping sequences means that we do something to them to make sure they are recognized as text, not part of the code.

In JavaScript, we do this by putting a backslash (\) just before the character.

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

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

Run code

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

Run code

The sequence \\ inserts a backslash in a string:

Run code

Six other escape sequences are valid in JavaScript:

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

Escape sequences are also useful for situations where you want to use characters that can't be typed using a keyboard.


Breaking Long Code Lines

For best readability, you should 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:

Run code

Writing a very long string on a single line will quickly become very hard to read and work with.

We can use the concatenation operator (+) to show the string on multiple lines.

Run code

Instead of concatenating multiple strings, we can use the \ escape character.

Run code

Note: The (\) method is not preferred, as it may cause issues with some browsers and minifiers.

In order to make code more readable, we can instead use template literal strings. These eliminate the need for concatenation or escaping on long strings.

Run code

It's important to be aware of all the ways of creating strings that span across multiple lines, as different code bases may be using various standards.


String Primitives and String Objects

Normally, JavaScript strings are primitive values, created from literals:

var city = "New Delhi";

But strings can also be defined as objects using the new keyword:

var city = new String("New Delhi");

In order to test the difference between the two, we will initialize a string primitive and a string object.

Run code

Note: Don't create strings as objects. It slows down execution speed and can produce some unexpected results.

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

Run code

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

Run code

Objects cannot be compared:

Run code

Notice the difference between (==) and (===). Comparing two JavaScript objects will always return false.