SyntaxStudy
Sign Up
JavaScript Creating Strings in JavaScript
JavaScript Beginner 6 min read

Creating Strings in JavaScript

Creating Strings in JavaScript

A string is an ordered sequence of characters used to represent text. JavaScript provides three distinct syntaxes for creating string literals, and understanding when to use each is fundamental to writing clean, readable code.

Single and Double Quotes

Single-quoted and double-quoted strings behave identically. The only practical difference is which quote character you must escape inside each. Use single quotes when your string contains double quotes, and vice versa.

  • 'Hello, world!' — single-quoted
  • "Hello, world!" — double-quoted
  • Escape a quote with a backslash: 'it\'s fine'

Template Literals

Template literals use backticks (`) and unlock two powerful features: multi-line strings without escape sequences, and embedded expressions via ${ } placeholders. Any valid JavaScript expression can appear inside the placeholder, including function calls and arithmetic.

String Immutability

Strings in JavaScript are immutable — once created, their characters cannot be changed in place. Operations that appear to modify a string actually return a new string, leaving the original untouched.

Example
const single = 'Hello';
const double = "World";
const name = 'Alice';
const greeting = "Hello, " + name + "!";
const multiLine = `Line one
Line two`;
const expr = `2 + 2 = ` + (2 + 2);
console.log(greeting);  // Hello, Alice!
console.log(multiLine);
console.log(expr);       // 2 + 2 = 4
Pro Tip

Use template literals whenever you need to embed variables or expressions — they eliminate messy string concatenation and make the intent of your code obvious at a glance.