๐ฏ By the end of this lesson you canโฆ
- Print text and numbers
- Print several values in one line
- Use template strings to mix text and values
Printing different things
โก JavaScript
printing.js
๐ Tap a line to explain it
1console.log("I love mangoes ๐ฅญ");2console.log(42);3console.log("Age:", 12);4console.log("Sum:", 7 + 8, "Product:", 7 * 8);
Template strings
Backticks ` ` (the key under Esc) let you drop values straight into text with ${ }`.
โก JavaScript
template.js
๐ Tap a line to explain it
1const name = "Meera";2const apples = 3;3console.log(`${name} has ${apples} apples.`);4console.log(`Double is ${apples * 2}.`);
console.log as an X-ray
When a program gives a strange answer, print the values in the middle. It is like an X-ray: you see what the computer really has, not what you think it has.
โก JavaScript
xray.js
๐ Tap a line to explain it
1const price = 250;2const discount = 0.1;3console.log("discount is", discount);4console.log("final:", price - price * discount);
๐ฎ What will this display?
What does this print?
1const n = 4;2console.log(`${n} + ${n} = ${n + n}`);
๐ ๏ธ Movie ticket
Store movie = "Space Kids" and seats = 3. Using a template string, print 3 seats booked for Space Kids.
๐ Key takeaways
- Commas in
console.log(a, b)print values separated by spaces. - Backticks make template strings: `
Score: ${score}`. - Programmers print values all the time to check what the program is doing.
Finished reading & practising?
Saved in this browser. Create a free account to keep it forever.