JavaScript Tutorial in Hindi #15 | Strings in JavaScript (2025)
1. What is a String in JavaScript?
A string is a sequence of characters used to represent text.
Example:
js
CopyEdit
let name = "Mohit";
**2. Declaring Strings using ' '
, " "
, and ` `
'Single Quotes'
"Double Quotes"
`Backticks`
(Template Literals)
All three create strings. Use the one that fits your content best.
3. Difference Between Quotes and When to Use Which
' '
or" "
: Regular strings.` `
: Template literals – allow multiline strings and embed variables using${}
.
✅ Use backticks `
when:
- You need variable insertion
- You want multiline strings
4. String Concatenation using +
and Template Literals
Using +
:
js
CopyEdit
let name = "Mohit";
let message = "Hello, " + name + "!";
Using Template Literals:
js
CopyEdit
let message = `Hello, ${name}!`;
✅ Template literals are cleaner and easier to read.
5. Dynamic String Insertion
You can insert variables or expressions directly using template literals:
js
CopyEdit
let product = "laptop";
let price = 50000;
let info = `The ${product} costs ₹${price}`;
6. Using Escape Characters (\n
, \"
, \'
, \\
)
CharacterMeaning | |
\n | New line |
\' | Single quote |
\" | Double quote |
\\ | Backslash |
Example:
js
CopyEdit
let str = "He said, \"Hello\"\nAnd left.";
7. Real-Life Examples with Template Literals
✅ Email Template:
js
CopyEdit
let user = "Mohit";
let msg = `Hi ${user},
Welcome to Mohit Decodes!
Have a great day!`;
console.log(msg);
✅ Bill Receipt:
js
CopyEdit
let item = "Coffee";
let price = 120;
let receipt = `You ordered: ${item}
Total: ₹${price}`;
console.log(receipt);