JavaScript: Arithmetic operations
At a basic level, computers only work with numbers. Even if you write a complex application in a modern programming language, numerous calculations are always happening inside it: addition, subtraction, division, and so on.
Fortunately, to start programming, it is enough to know ordinary school arithmetic. That is where we will begin.
Addition in JavaScript
In math, we write 3 + 4 to add. In JavaScript, it is exactly the same:
3 + 4;This code can actually be run: the interpreter will perform the calculation. But... it will do nothing with the result. That is, 7 will be produced, but you will not see it.
To see the result, you need to print it
In a real program, simply computing a value is not enough. You need to do something with the result, for example, show it to the user.
To do this, we use the now familiar console.log() command:
// First the sum is computed,
// then it is passed to the print function
console.log(3 + 4);console.log(3 + 4)
└─┬─┘
7
console.log(7) → 7Execution result:
7If we write the same expression as a string, we get a completely different result — the string will be printed "as is":
console.log('3 + 4'); // prints: 3 + 4
console.log(3 + 4); // prints: 7Other arithmetic operations
JavaScript supports all the usual operations plus a few specific ones related to how numbers are stored and processed on a computer:
| Operation | Symbol | Example | Result |
|---|---|---|---|
| Addition | + | 2 + 3 | 5 |
| Subtraction | - | 7 - 2 | 5 |
| Multiplication | * | 4 * 3 | 12 |
| Division | / | 8 / 2 | 4 |
| Exponentiation | ** | 3 ** 2 | 9 |
| Remainder | % | 7 % 3 | 1 |
Here is how you can print the result of division and exponentiation:
console.log(8 / 2); // => 4
console.log(3 ** 2); // => 9What is the remainder of a division (%)
This operation is called taking the remainder of a division. It shows what is "left over" when one number is divided by another not evenly. Example:
console.log(7 % 3); // => 1Why is the result equal to 1?
- 7 is divided by 3 twice: 3 * 2 = 6
- Up to 7 there is 1 left, and that is the remainder.
Other examples:
console.log(10 % 4); // => 2 (10 is divided by 4 twice: 4 * 2 = 8, remainder 2)
console.log(15 % 5); // => 0 (divides evenly)The % operation is often used in programming, for example, to check whether a number divides evenly (if the remainder is 0).
Formatting arithmetic expressions
From JavaScript's point of view, there is no difference between 3+4 and 3 + 4. The interpreter will understand both options the same way. The only difference is in code formatting. In programming, it is conventional to put spaces around arithmetic operators, because that makes expressions easier to read:
console.log(3 + 4);
console.log(8 / 2);
console.log(7 % 3);Instructions
You walked into a wholesale store and saw a pack of 9 batteries for 81 rubles. In a regular store, one battery costs 12 rubles. Calculate and print the price of a single battery in the wholesale pack to figure out where it is cheaper to buy.
Tips
Always separate arithmetic operators from the numbers (operands) with spaces – this is good programming style. That is why in our examples it is
console.log(3 + 4), notconsole.log(3+4).Division by zero is
Infinity. We will figure out what that means in future lessons.
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Your exercise will be checked with these tests:
// @ts-check
import { expect, test, vi } from 'vitest';
test('basic', async () => {
const consoleLogSpy = vi.spyOn(console, 'log');
await import('./index.js');
const firstArg = consoleLogSpy.mock.calls.join('\n');
expect(firstArg).toBe('9');
});Teacher's solution will be available in:
20:00
