THE WORLD'S LARGEST WEB DEVELOPER SITE

JS Tutorial

JS HOME JS Introduction JS Where To JS Output JS Statements JS Syntax JS Comments JS Variables JS Operators JS Arithmetic JS Assignment JS Data Types JS Functions JS Objects JS Events JS Strings JS String Methods JS Numbers JS Number Methods JS Arrays JS Array Methods JS Array Sort JS Array Iteration JS Dates JS Date Formats JS Date Get Methods JS Date Set Methods JS Math JS Random JS Booleans JS Comparisons JS Conditions JS Switch JS Loop For JS Loop While JS Break JS Type Conversion JS Bitwise JS RegExp JS Errors JS Scope JS Hoisting JS Strict Mode JS this Keyword JS Let JS Const JS Arrow Function JS Classes JS Debugging JS Style Guide JS Best Practices JS Mistakes JS Performance JS Reserved Words JS Versions JS Version ES5 JS Version ES6 JS JSON

JS Forms

JS Forms Forms API

JS Objects

Object Definitions Object Properties Object Methods Object Accessors Object Constructors Object Prototypes Object ECMAScript 5

JS Functions

Function Definitions Function Parameters Function Invocation Function Call Function Apply Function Closures

JS HTML DOM

DOM Intro DOM Methods DOM Document DOM Elements DOM HTML DOM CSS DOM Animations DOM Events DOM Event Listener DOM Navigation DOM Nodes DOM Collections DOM Node Lists

JS Browser BOM

JS Window JS Screen JS Location JS History JS Navigator JS Popup Alert JS Timing JS Cookies

JS AJAX

AJAX Intro AJAX XMLHttp AJAX Request AJAX Response AJAX XML File AJAX PHP AJAX ASP AJAX Database AJAX Applications AJAX Examples

JS JSON

JSON Intro JSON Syntax JSON vs XML JSON Data Types JSON Parse JSON Stringify JSON Objects JSON Arrays JSON PHP JSON HTML JSON JSONP

JS vs jQuery

jQuery Selectors jQuery HTML jQuery CSS jQuery DOM

JS Examples

JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Exercises JS Quiz JS Certificate

JS References

JavaScript Objects HTML DOM Objects


JavaScript Strings


JavaScript strings are used for storing and manipulating text.


JavaScript Strings

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

Example

var x = "John Doe";
Try it Yourself »

You can use single or double quotes:

Example

var carName1 = "Volvo XC60";  // Double quotes
var carName2 = 'Volvo XC60';  // Single quotes
Try it Yourself »

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

Example

var answer1 = "It's alright";
var answer2 = "He is called 'Johnny'";
var answer3 = 'He is called "Johnny"';
Try it Yourself »

String Length

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

Example

var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;
Try it Yourself »


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

var x = "We are the so-called \"Vikings\" from the north.";
Try it Yourself »

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

Example

var x = 'It\'s alright.';
Try it Yourself »

The sequence \\  inserts a backslash in a string:

Example

var x = "The character \\ is called backslash.";
Try it Yourself »

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

document.getElementById("demo").innerHTML =
"Hello Dolly!";
Try it Yourself »

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

Example

document.getElementById("demo").innerHTML = "Hello \
Dolly!";
Try it Yourself »

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

document.getElementById("demo").innerHTML = "Hello " +
"Dolly!";
Try it Yourself »

You cannot break up a code line with a backslash:

Example

document.getElementById("demo").innerHTML = \
"Hello Dolly!";
Try it Yourself »

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

var x = "John";
var y = new String("John");

// typeof x will return string
// typeof y will return object
Try it Yourself »

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

var x = "John";             
var y = new String("John");

// (x == y) is true because x and y have equal values
Try it Yourself »

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

Example

var x = "John";             
var y = new String("John");

// (x === y) is false because x and y have different types (string and object)
Try it Yourself »

Or even worse. Objects cannot be compared:

Example

var x = new String("John");             
var y = new String("John");

// (x == y) is false because x and y are different objects
Try it Yourself »

Example

var x = new String("John");             
var y = new String("John");

// (x === y) is false because x and y are different objects
Try it Yourself »

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


Test Yourself With Exercises

Exercise:

Use the length property to alert the length of txt.

var txt = "Hello World!";
var x = ;
alert(x);

Start the Exercise