Your first program
Statements, output and comments
A JavaScript program is a list of statements — instructions — that run from top to bottom.
console.log("Welcome to Corner Café!");
console.log("Today's special: banana bread");
console.log(2 + 3);Console output:
Welcome to Corner Café!
Today's special: banana bread
5
Breaking it down
console.log(…)prints whatever is in the brackets to the console. You'll use it constantly.- Text goes inside quotes:
"like this"or'like this'. Text in code is called a string. - Numbers don't need quotes — and JavaScript can do maths with them.
- Each statement ends with a semicolon
;. It's optional in many cases, but using it is a good habit.
Comments: notes for humans
Anything after // on a line is a comment. JavaScript ignores it — it's for you and other people reading the code.
// This line is a comment, so it doesn't run
console.log("This line runs"); // comments can go at the end tooConsole output:
This line runs
Good comments explain why you did something, not what every line does.
Spelling and capitals matter
JavaScript is case-sensitive. console.log works; Console.Log and console.log with a typo don't. Brackets and quotes must also come in pairs.
Try it
In the playground, write a program that prints three lines: your name, your favourite food, and the answer to 365 * 24 (the hours in a year). Add a comment above it saying what the program does.
