String Literals: Single and Double Quotes
What you'll learn: How to create strings in JavaScript using either single or double quotes, and when each style is most useful.
What Are String Literals?
A string literal is simply the way you write text values directly in your code. In JavaScript, you can wrap text in either single quotes (') or double quotes ("), and both create valid strings:
let greeting1 = 'Hello, world!';
let greeting2 = "Hello, world!";
Both of these are completely equivalent — they create the exact same string value. Think of quotes like wrapping paper: whether you use blue or red paper, the gift inside is the same.
When to Choose Single vs Double Quotes
The choice usually comes down to personal preference or team style guidelines. However, one practical reason to prefer one over the other is when your string contains a quote character:
let sentence1 = "It's a beautiful day!"; // Easy! No escaping needed
let sentence2 = 'She said, "Hello!"'; // Easy! No escaping needed
If you try to use the same quote type inside the string as outside, you'll create a syntax error. The outer quotes define where the string begins and ends, so matching quotes inside confuse JavaScript.
Consistency Matters
While JavaScript doesn't care which you choose, pick one style and stick with it throughout your code. Many developers prefer single quotes because they require one less keystroke and look cleaner, but double quotes are equally valid.
Key Takeaway: JavaScript strings can use single (') or double (") quotes interchangeably — both create identical string values. Choose the outer quote style that doesn't match any quotes inside your text.