Template Literals
Drop variables straight into text, the modern way.
1const name = "Ada";
2const age = 10;
3console.log(`${name} is ${age} years old`);
$ node main.js
Ada is 10 years old
Every line, explained
const name = "Ada";- const creates a variable: a named box that stores a value. const is short for "constant": once a value is stored, that name can never be given a new value. Modern JavaScript uses const for most things. The semicolon ; marks the end of the statement.
const age = 10;- const creates a variable: a named box that stores a value. const is short for "constant": once a value is stored, that name can never be given a new value. Modern JavaScript uses const for most things. The semicolon ; marks the end of the statement. Numbers don't need quotes: 10 is a number you can do maths with, "10" would be text.
console.log(`${name} is ${age} years old`);- The backtick quotes ` (usually next to the 1 key) make a template literal. Inside one, ${...} is magic: JavaScript swaps ${name} for the value of name. This is the modern way to mix variables into text, much tidier than gluing pieces with +. Here ${name} becomes Ada and ${age} becomes 10.