JavaScript / Getting started

Calculations and text

Make the computer calculate with + - * / %, and join text with calculation results in one line.

Computers are very fast at arithmetic, even with huge numbers. To make one calculate, write the sum inside console.log() without quotes. Anything inside quotes is text and is printed exactly as written:

JavaScript
console.log("1+1");
console.log(1+1);
console.log(1400 + 3600);
Console
1+1
2
5000

Adding and subtracting use the usual signs, but keyboards have no × or ÷, so multiplication is * and division is /. There is a fifth useful sign: % gives the remainder of a division.

OperationSymbolExampleResult
Add+1400 + 36005000
Subtract-100 - 5050
Multiply*5 * 630
Divide/10 / 52
Remainder%17 % 52
JavaScript
console.log(5 * 9);
console.log(10 / 2);
console.log(7 / 2);
console.log(17 % 5);
Console
45
5
3.5
2

Just like in maths, *, / and % happen before + and -, and anything in brackets ( ) is calculated first. % is handy for checking even numbers: an even number divided by 2 leaves a remainder of 0.

JavaScript
console.log(2 + 3 * 4);
console.log((2 + 3) * 4);
console.log(10 % 2);
console.log(11 % 2);
Console
14
20
0
1

Joining text with +

Put + between two pieces of text and it joins them into one text instead of adding. Watch the spaces: the computer never adds a space for you.

JavaScript
console.log("Today's lunch is " + "koshary.");
console.log("Good" + "morning");
console.log("Good " + "morning");
Console
Today's lunch is koshary.
Goodmorning
Good morning

To put a result inside a sentence, use the pattern "text " + (formula) + " text". The brackets around the formula make the computer calculate it first, then join the result to the text:

JavaScript
console.log("Set of 2 coffees " + (250 * 2) + " pounds");
console.log("Set of 3 coffees " + (250 * 3) + " pounds");
Console
Set of 2 coffees 500 pounds
Set of 3 coffees 750 pounds
Do not forget the brackets around an addition inside a sentence. Without them the computer joins each number to the text one after the other, printing Total: 23 instead of Total: 5:
JavaScript
console.log("Total: " + 2 + 3);
console.log("Total: " + (2 + 3));
Console
Total: 23
Total: 5

Practice

1Write the programCurriculum practice

Make the computer calculate 5 × 6 and print the result.

HintMultiplication is written *.
JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
2Write the programCurriculum practice

Make the computer calculate 15 ÷ 5 and print the result.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
3Fill in the blankCurriculum practice

Fill in the blank so the program prints Two muffins 640 pounds, with the computer working out 640 from 320 × 2.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
console.log("Two muffins " + ___ + " pounds");
Console
Press Run to see the result.
4Edit the programCurriculum practice

This program prints the sum as text. Change it so the computer calculates 2 × 5 and prints The answer is 10.

HintTake the sum out of the quotes: "text " + (formula) + "."
JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
console.log("The answer is 2 * 5.");
Console
Press Run to see the result.
5Fill in the blankCurriculum practice

Bread costs 200 and coffee costs 150. Fill in the blank to print Bread and coffee for 350 pounds, letting the computer calculate 200 + 150.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
console.log("Bread and coffee for " + ___ + " pounds");
Console
Press Run to see the result.
6Write the programExtra practice

A class has 23 students and the teacher splits them into groups of 4. Print Students left over: 3, letting the computer find the remainder with %.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
7Write the programExtra practice

You buy 3 notebooks at 12 pounds each and 2 pens at 5 pounds each. Print Total: 46 pounds, letting the computer work out the total in one formula.

HintMultiplication happens before addition, so you need no extra brackets inside the formula.
JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.

Key points

  • A calculation is written without quotes: console.log(5 * 6);
  • Signs: + - * /, and % gives the remainder; * and / come before + and -.
  • + between texts joins them; to put a result in a sentence: "text " + (formula) + " text".