Back to course

The String Type: Single, Double, and Escaping Quotes

JavaScript: The Complete '0 to Hero' Beginner Course

14. The String Type

A String is used to store text. In JavaScript, strings can be defined using single quotes (') or double quotes (").

Defining Strings

They are treated identically, but consistency is key.

javascript let single = 'This is a string using single quotes.'; let double = "This is a string using double quotes.";

Escaping Quotes

If you need to include the same type of quote inside the string, you must use a backslash (\) to 'escape' it.

javascript // Using single quotes inside a double-quoted string is easier: let quote1 = "The developer said 'It works!'";

// Escaping a quote: let quote2 = 'It's raining today.'; // The backslash cancels the quote's effect.

String Concatenation

You combine strings using the + operator.

javascript let greeting = 'Hello'; let name = 'Maria';

let fullMessage = greeting + ' ' + name + '!'; console.log(fullMessage); // Output: Hello Maria!