Hello, World!
Programming Languages · JavaScript Basics · 8 min
Make JavaScript talk: print text and numbers, and leave notes for humans.
Your first line of code
A program is a list of instructions for the computer. In JavaScript, console.log() shows whatever you put inside the brackets.
Text goes inside quotes, so JavaScript knows it's text and not code. A ; at the end marks the end of the instruction.
console.log("Hi there");
Print
Hello, World!
One log, one line
Each console.log() prints on a new line. JavaScript runs your lines from top to bottom, in order, like reading a recipe.
console.log("Step 1");
console.log("Step 2");
Print
I am learning JavaScripton the first line.On the next line, print
It is fun.
Numbers don't need quotes
Text in quotes is called a string. Numbers are different: leave out the quotes and JavaScript can do maths with them.
console.log(7);
console.log(2 + 3);
The second line prints 5, not 2 + 3. With quotes, console.log("2 + 3") would print the text exactly as written.
Print the result of
12 * 4(no quotes).
Comments are notes for humans
A line that starts with // is a comment. JavaScript skips it completely. Programmers use comments to explain why the code does something.
// This line is ignored
console.log("This one runs");
Add a comment that says what the program does.
Below it, print
Comments are skipped.
Put it together: a scoreboard
Time to combine everything: a comment, some text and a bit of maths. Let JavaScript do the adding for you.
Add a comment at the top that says what the program does.
Print
Scoreboard.On the next line, print the total of
10 + 25 + 7(let JavaScript add it up).
More in JavaScript Basics
Previous: Dataclasses and Typing · Next: Variables