Learning object 4 of 100
Template literal builder
Fill in values and a template, and see the exact line JavaScript produces, alongside the equivalent + concatenation and console.log with commas.
Loading…
What it shows
Three ways to build output: + joins strings exactly (you add the spaces), commas in console.log add a space between values, and template literals (backticks) insert values with ${ }. Lesson 7 covers template literals in depth.
Guided experiment
- Change the quantity to 4 and watch all three outputs update.
- Remove a space in the template. Which of the three outputs changes?
- Put an expression inside ${ }, such as
${price * quantity}.
Predict
With item = "Pen" and price = 2, what does ` ${item}: ${price * 3} ` produce?
Related
Lessons: Lesson 4: Using the console and understanding output, Lesson 7: Strings and template literals
Projects: Project 9: Shopping Cart Price Calculator
Source: web/assets/js/objects/object-004.js in the companion repository.