Last updated

JavaScript Strings

Definition: A string is text, written in single quotes, double quotes, or backticks.

let a = "Hello";
let b = 'World';
console.log(a, b);

Example 1 — joining strings

let first = "Talented";
let last = "Hub";
console.log(first + " " + last);

Example 2 — template literals (backticks)

Backticks let you drop variables straight into text with ${...} — the cleanest way to build messages:

let name = "Sam";
let age = 20;
console.log(`${name} is ${age} years old`);

Example 3 — length and methods

let text = "JavaScript";
console.log(text.length);        // 10
console.log(text.toUpperCase()); // JAVASCRIPT
console.log(text.toLowerCase()); // javascript
console.log(text.replace("Java", "Type"));

Example 4 — getting and slicing characters

let word = "Hello";
console.log(word[0]);        // H
console.log(word.charAt(1)); // e
console.log(word.slice(0, 3)); // Hel

💡 Tip: template literals (backticks) are usually nicer than joining with +.

Try it Yourself
Output

          
Ad · responsive