Lesson 1 of 11JavaScript
JavaScript · Lesson 1 · 25 minutes
Hello, JavaScript!
What you'll learn
- Print something with
console.log() - Create variables with
letandconst - Build text with template strings
JavaScript – the language of the web
Every website that moves, reacts to clicks or offers games in the browser uses JavaScript. It runs in every browser – this editor runs your code directly too.
Not related to Java: despite the name, JavaScript has almost nothing to do with the language Java. In 1995 the name was a marketing trick because Java was all the rage.
Your first program
In JavaScript, printing is called console.log():
console.log("Hello, world!");
console.log(7 * 6);
console.log("I am", 12, "years old");Most statements end with a semicolon ;. It’s often not required, but it’s good style.
Variables: let and const
const name = "Mia"; // const: never changes
let points = 10; // let: may change
points = points + 5;
console.log(name, points);constfor values that stay the same – that’s the normal case.letwhen the value should change later.
Try changing name later – JavaScript complains!
Building text
You can join text with +. More convenient are template strings in backticks (`). Inside them, ${…} inserts values:
const animal = "cat";
const count = 3;
console.log("I have " + count + " " + animal + "s.");
console.log(`I have ${count} ${animal}s.`);
console.log(`In 5 years I'll be ${12 + 5}.`);Python comparison: template strings are like f-strings in Python:
f"I am {age}"↔`I am ${age}`.
Quick check
What does this program print?
let x = 4;
x = x * 2 + 1;
console.log(`x is ${x}`);Your task
Your turn
Create two constants: name with your name and age with your age. Then print exactly this sentence with one template string (backticks ` ` and ${…}): My name is Mia and I am 12 years old.` – with your values, of course.
// Create name and age here
console.log("Hello!");- Something to think about
For example, write
constname="Mia"; andconstage=12; - Which command helps?
Watch every character – including the full stop at the end.
A template string is written in backticks:
`My name is ${name} …` - Gap code – fill in the ___
const name = ___; const age = ___; console.log(___);
Solve it without hints to earn a ⭐ on your progress page.
Show solution🔒 Unlocks after all hints or 2 checks
Try it yourself first – mistakes teach you the most. Here is one possible solution:
const name = "Mia";
const age = 12;
console.log(`My name is ${name} and I am ${age} years old.`);