${}' in javascript

Can anyone tell me what ${}’ does in javascript?

Lesson:
https://www.codecademy.com/courses/introduction-to-javascript/lessons/javascript-iterators/exercises/for-each?action=resume_content_item

Sorry I typed it wrong it’s actually

${}.

Check out this link for a detailed explanation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

The short answer is that the ${myVariable} syntax is part of the Template Literals feature added to JavaScript in ES6. The ${ } allows us to insert variables directly into strings without having to use concatenation:

//concatenation
 console.log('My dog is named ' + myDogName + '. He is ' + myDogAge + ' years old.');

Using a template literal we can just to this:

//template literal
console.log(`My dog is named ${myDogName}. He is ${myDogAge} years old.`);

Hope this helps!

1 Like

Just to expand upon this, it permits us to insert any expression, not just a variable.

const isEven = n => n % 2 === 0;
x = 17
console.log(`${x} is ${isEven(x) ? `Even` : `not Even`}.`)
4 Likes