used for holding text
3 ways to create strings:
using single quotes:
const first = 'Soumya';
using double quotes:
const middle = "Ranjan";
using backticks:
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:
const song = 'Oh \\
I like \\
pizza';
console.log(song); // Oh I like pizza
const song = `Oh
I like
pizza`;
console.log(song);
/*
Oh
I like
pizza
*/
2nd one using backticks is mostly used.
String concatenation and interpolation
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.
Backticks also used for tagged template literals.
Backticks are helpful for creating HTML:
const html = `
<div>
<h2>Hey everyone! I am ${name}.</h2>
</div>
`;