-1

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

halfer
  • 19,824
  • 17
  • 99
  • 186
Izza Ijaz
  • 9
  • 1
  • In the first case you're (presumably) using a [template string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) - in the second string concatenation. – Andy Jul 26 '23 at 03:30

1 Answers1

2

Both are valid. First one is called string template.

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122