Category: javascript
What is a String Literal in JavaScript?
Published on 15 Feb 2026
Explanation
What is a String Literal in JavaScript?
A string literal is a sequence of characters enclosed in quotes.
It represents text in JS and is used to store or display textual data.
There are three types of string literals:
1. Single quotes `' '`
2. Double quotes `" "`
3. Backticks `` ` ` `` (Template literals)
Code:
Explanation
Single and Double Quotes
Both work the same way.
let name1 = 'John';
let name2 = "Alice";
console.log(name1); // John
console.log(name2); // Alice
cont..
Code:
Explanation
cont..
Use `'` or `"` interchangeably.
To include quotes inside a string
Code:
let greeting = "He said, 'Hello!'"; console.log(greeting); // He said, 'Hello!'
Explanation
Escaping Characters
Use backslash `\` to escape characters.
Code:
let text = 'It\'s a sunny day'; console.log(text); // It's a sunny day let path = "C:\\Users\\John"; console.log(path); // C:\Users\John
Explanation
Template Literals (Backticks)
Backticks `` ` ` `` allow:
Multi-line strings
String interpolation (embed variables)
Expressions inside `${ }`
Code:
let message = `Hello, This is a multi-line string.`; console.log(message); Output: Hello, This is a multi-line string.
Explanation
String Interpolation
Code:
let name = "Alice";
let age = 25;
let info = `My name is ${name} and I am ${age} years old.`;
console.log(info);
Output:
My name is Alice and I am 25 years old.
Explanation
Expression Inside Template Literal
Code:
let a = 5;
let b = 10;
console.log(`Sum of a + b = ${a + b}`);
Output:
Sum of a + b = 15
Explanation
Common String Methods
Code:
let str = "Hello World";
console.log(str.length);
// 11
console.log(str.toUpperCase());
// "HELLO WORLD"
console.log(str.toLowerCase());
// "hello world"
console.log(str.includes("World"));
// true
console.log(str.split(" "));
// ["Hello", "World"]
console.log(str.replace("World", "JavaScript"));
// "Hello JavaScript"
Explanation
Summary Table
Code:
String Type
Single quotes
`'Hello'`
Double quotes
`"Hello"`
Backticks (template literal)
`` `Hello ${name}` ``
Multi-line, interpolation, expressions
Explanation
Real-time Example
Code:
let user = {
name: "John",
score: 95
};
let message = `Hello ${user.name},
Your score is ${user.score}.
Congratulations!`;
console.log(message);
Output:
Hello John,
Your score is 95.
Congratulations!