- used for holding text
- 3 ways to create strings:
- using single quotes:
- using double quotes:
- using backticks:
const first = 'Soumya';
const middle = "Ranjan";
const last = `Mohanty`;
- single quotes and double quotes are the same thing.
used for:
"she's cool"
or escaping:
'she\'s cool'
- backticks:
const sentence = `she's so "cool"`;
console.log(sentence); // she's so "cool"
- Multi-line string:
1.
const song = 'Oh \
I like \
pizza';
console.log(song); // Oh I like pizza
2.
const song = `Oh
I like
pizza`;
console.log(song);
/*
Oh
I like
pizza
*/
2nd one using backticks is mostly used.
- String concatenation and interpolation
- '+' is used for concatenation. It is also used for adding 2 nos.
- Concatenation: when 2 or more strings combined to one
- Interpolation: when you put a variable inside a string
- Example 1:
- Example 2:
const name = 'Soumya';
const hello = 'Hello my name is ' + name + '. Nice to meet you.'
(can use double or single quotes)
1+1 // 2
'1'+'1' // 11
1 + '1' // 11
const name = 'Soumya';
const hello = `Hello my name is ${name}. Nice to meet you. I am ${100+1} years old.`;
console.log(hello); // Hello my name is Soumya. Nice to meet you. I am 101 years old.
const html = `
<div>
<h2>Hey everyone! I am ${name}.</h2>
</div>
`;