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:
console.log("1+1");
console.log(1+1);
console.log(1400 + 3600);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.
| Operation | Symbol | Example | Result |
|---|---|---|---|
| Add | + | 1400 + 3600 | 5000 |
| Subtract | - | 100 - 50 | 50 |
| Multiply | * | 5 * 6 | 30 |
| Divide | / | 10 / 5 | 2 |
| Remainder | % | 17 % 5 | 2 |
console.log(5 * 9); console.log(10 / 2); console.log(7 / 2); console.log(17 % 5);
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.
console.log(2 + 3 * 4); console.log((2 + 3) * 4); console.log(10 % 2); console.log(11 % 2);
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.
console.log("Today's lunch is " + "koshary.");
console.log("Good" + "morning");
console.log("Good " + "morning");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:
console.log("Set of 2 coffees " + (250 * 2) + " pounds");
console.log("Set of 3 coffees " + (250 * 3) + " pounds");Set of 2 coffees 500 pounds Set of 3 coffees 750 pounds
console.log("Total: " + 2 + 3);
console.log("Total: " + (2 + 3));Total: 23 Total: 5
Practice
Make the computer calculate 5 × 6 and print the result.
Hint
Multiplication is written*.Press Run to see the result. Make the computer calculate 15 ÷ 5 and print the result.
Press Run to see the result. Fill in the blank so the program prints Two muffins 640 pounds, with the computer working out 640 from 320 × 2.
console.log("Two muffins " + ___ + " pounds");Press Run to see the result. This program prints the sum as text. Change it so the computer calculates 2 × 5 and prints The answer is 10.
Hint
Take the sum out of the quotes:"text " + (formula) + "."console.log("The answer is 2 * 5.");Press Run to see the result. 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.
console.log("Bread and coffee for " + ___ + " pounds");Press Run to see the result. 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 %.
Press Run to see the result. 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.
Hint
Multiplication happens before addition, so you need no extra brackets inside the formula.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".