Strings
A string is text, written between quotes, and it is the type you will handle more often than any other.
Single quotes, double quotes and backticks all make strings. Backticks add template literals, which let you drop values straight into text with a dollar sign and braces.
EXERCISE
1Three Ways to Quote Text
JavaScript accepts three different quote characters for strings, and the third one can do something the other two cannot.
A string is any text between quotes. The quotes are not part of the value, they are how you tell where the text starts and stops.
All three of these create a string:
const a = 'single quotes';
const b = "double quotes";
const c = `backticks`;
console.log(typeof a, typeof b, typeof c); // string string string
console.log(a === 'single quotes'); // true
Choosing the quote lets you use the other one inside:
const line1 = "It's a good day"; // apostrophe inside double quotes
const line2 = 'She said "hello"'; // double quotes inside single
// If you need the same quote inside, escape it with a backslash:
const line3 = 'It\'s fine';
const line4 = "She said \"hello\"";
Backticks can span lines, the others cannot:
const address = `12 Church Street
Bengaluru
560001`;
console.log(address);
// 12 Church Street
// Bengaluru
// 560001
// const broken = "12 Church Street
// Bengaluru";
// SyntaxError: Invalid or unexpected token
Single and double quotes are interchangeable, so pick one and stay consistent. Backticks are not just a third option, they are a more capable kind of string, and the next exercise shows the reason most developers now reach for them by default.
EXERCISE
2Template Literals Put Values Inside Text
Backticks let you drop a variable straight into the middle of a sentence, which is far easier to read than joining pieces with plus signs.
Joining text and values with + works, and it gets ugly fast. Backticks fix that with ${ }.
The old way, and the readable way:
const name = "Ada";
const age = 36;
// Concatenation: mind every space and every plus
const oldWay = "Hi " + name + ", you are " + age + " years old.";
// Template literal: read it straight through
const newWay = `Hi ${name}, you are ${age} years old.`;
console.log(oldWay === newWay); // true
console.log(newWay); // Hi Ada, you are 36 years old.
Anything that produces a value can go inside the braces:
const price = 250;
const qty = 3;
console.log(`Total: ${price * qty}`); // Total: 750
console.log(`Name in caps: ${name.toUpperCase()}`); // Name in caps: ADA
console.log(`Status: ${age >= 18 ? "adult" : "minor"}`); // Status: adult
const user = { city: "Pune" };
console.log(`Lives in ${user.city}`); // Lives in Pune
A trap worth seeing once:
// The braces are required. A bare dollar sign does nothing.
console.log(`Hi $name`); // Hi $name
console.log(`Hi ${name}`); // Hi Ada
// And ${ } only works inside BACKTICKS.
console.log("Hi ${name}"); // Hi ${name}
Pro Tip: Use backticks by default. They read like the sentence you are actually building, they handle multiple lines, and they remove the entire class of bugs where a missing space in
"Hi " + nameproducesHiAda. The only cost is one unfamiliar character on your keyboard.
EXERCISE
3Strings Are Values, Not Containers You Edit
Every string method gives you a brand new string back and leaves the original exactly as it was, which surprises people the first time.
Strings are immutable. You cannot change one. Every method that looks like it edits a string actually returns a new one.
The method returns; the original stays:
const greeting = "hello";
greeting.toUpperCase();
console.log(greeting); // hello <- nothing happened
// The new string was created and thrown away.
// You have to catch it:
const shouted = greeting.toUpperCase();
console.log(shouted); // HELLO
console.log(greeting); // hello <- still untouched
You cannot write into a position either:
const word = "cat";
console.log(word[0]); // c reading a position works
word[0] = "b"; // silently does nothing
console.log(word); // cat
// To get "bat", build a new string:
const changed = "b" + word.slice(1);
console.log(changed); // bat
Reassigning the variable is a different thing entirely:
let text = "hello";
text = text.toUpperCase(); // allowed: the LET is being reassigned
console.log(text); // HELLO
const fixed = "hello";
fixed = fixed.toUpperCase();
// TypeError: Assignment to constant variable.
// The string was never the problem. The const was.
Immutability is why string methods are safe to use anywhere: nothing you call on a string can damage it for the rest of your program. The practical rule is that a string method's return value is the whole point, so if you are not storing it or using it immediately, the call did nothing.
