A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://www.w3schools.com/js/js_strings.asp below:

Website Navigation


JavaScript Strings

JavaScript Strings

Strings are for storing text

Strings are written with quotes

Using Quotes

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

You can use single or double quotes:

Example

let carName1 = "Volvo XC60";  // Double quotes
let carName2 = 'Volvo XC60';  // Single quotes

Try it Yourself » Note

Strings created with single or double quotes work the same.

There is no difference between the two.

Quotes Inside Quotes

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

Example

let answer1 = "It's alright";
let answer2 = "He is called 'Johnny'";
let answer3 = 'He is called "Johnny"';

Try it Yourself » Template Strings

Templates were introduced with ES6 (JavaScript 2016).

Templates are strings enclosed in backticks (`This is a template string`).

Templates allow single and double quotes inside a string:

String Length

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

Example

let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let length = text.length;

Try it Yourself » Escape Characters

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

let text = "We are the so-called "Vikings" from the north.";

The string will be chopped to "We are the so-called ".

To solve this problem, you can use an backslash escape character.

The backslash escape character (\) turns special characters into string characters:

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

\" inserts a double quote in a string:

let text = "We are the so-called \"Vikings\" from the north.";

Try it Yourself »

\' inserts a single quote in a string:

let text= 'It\'s alright.';

Try it Yourself »

\\ inserts a backslash in a string:

let text = "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 Note

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 Lines

For readability, programmers often like to avoid long code lines.

A safe way to break up a statement is after an operator:

Example

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

Try it Yourself »

A safe way to break up a string is by using string addition:

Example

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

Try it Yourself » Template Strings

Templates were introduced with ES6 (JavaScript 2016).

Templates are strings enclosed in backticks (`This is a template string`).

Templates allow multiline strings:

JavaScript Strings as Objects

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

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

let y = new String("John");

Do not create String objects.

The new keyword complicates the code and slows down execution speed.

String objects can produce unexpected results:

When using the == operator, x and y are equal:

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

Try it Yourself »

When using the === operator, x and y are not equal:

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

Try it Yourself »

Note the difference between (x==y) and (x===y).

(x == y) true or false?

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

Try it Yourself »

(x === y) true or false?

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

Try it Yourself »

Comparing two JavaScript objects always returns false.

Track your progress - it's free!


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4