Izza Ijaz
Izza Ijaz

Reputation: 9

What does the dollar symbol do in a back-ticked JavaScript string?

Why we use $ sign why we can't print it like second program?

let animals = 'elephants, eagles, owls, and tigers';
console.log(`${animals.slice(11,17)} and ${animals.slice(19,23)} are cool birds`);

2nd program

let ani = 'elephants, eagles, owls, and tigers';
console.log(ani.slice(11,17) + ' ' + 'and'+ ' '+ ani.slice(19,23) + ' ' + 'are cool birds');

Outputs of both programs are same:

eagles and owls are cool birds
eagles and owls are cool birds

Upvotes: -1

Views: 45

Answers (1)

Mayank Kumar Chaudhari
Mayank Kumar Chaudhari

Reputation: 18806

Both are valid. First one is called string template.

Upvotes: 2

Related Questions