Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JS Strings

JavaScript Strings

Strings are used to store text and are enclosed in quotes.

Using Quotes

A JavaScript string consists of zero or more characters enclosed within quotes.

Example

let text = “John Doe”;

You can use either single or double quotes:

Example

let carName1 = “Volvo XC60”;  // Double quotes
let carName2 = ‘Volvo XC60’;  // Single quotes

Note

Strings created with single or double quotes function the same way; there is no difference between them.

Quotes Inside Quotes

You can include quotes within a string as long as they don’t match the quotes used to enclose the string.

Example

let answer1 = “It’s alright”;
let answer2 = “He is called ‘Johnny'”;
let answer3 = ‘He is called “Johnny”‘;

Template Strings

Templates were introduced in ES6 (JavaScript 2015) and are strings enclosed in backticks (`This is a template string`).

They allow the use of both single and double quotes within the string.

Example

let text = `He’s often called “Johnny”`;
Note: Templates are not supported in Internet Explorer.