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

32 Views Asked by At

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

1

There are 1 best solutions below

0
Mayank Kumar Chaudhari On

Both are valid. First one is called string template.