LESSON 02 / 07

Printing with console.log

console.log is your best friend: print text, numbers and several values at once, and use it to see inside your program.

๐ŸŽ“Beginnerlevel
โฑ๏ธ25 minto finish
โœ๏ธ2activities

๐Ÿ“‚ First Steps in JavaScript๐Ÿท๏ธ JavaScript basics

๐ŸŽฏ 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.