At the most basic level, computers only use numbers. Even in high-level language applications, there are many numbers and operations with them. To get started with programming all you need to know is basic arithmetic - let’s start with that.
When adding two numbers in math, we write, for example, 3 + 4. The same goes for programming. Here is a program that adds two numbers:
// Don't forget the semicolon at the end,
// since each line of code is a statement
3 + 4;
The statement 3 + 4 makes the computer add up the numbers and find the result. If you run this program, nothing will happen. Well, the computer will calculate the sum, of course, but that'll be it. The result of the sum isn't used, and as such, this program has no real value. We need to ask the computer to add 3 + 4, and then give it a command to do something with the result. For example, print it:
// The sum is calculated first,
// it is then passed to the print function
console.log(3 + 4); // => '7'
Always indent arithmetic operators with spaces between the numbers (operands), it's a good coding style. This is why we write console.log(3 + 4), and not console.log(3+4) in our examples.
Besides addition, the following operations are available:
*— multiplication/— division-— subtraction%— modulo**— exponentiation
Now let’s print the result of division, and then the result of exponentiation:
console.log(8 / 2); // => 4
console.log(3 ** 2); // => 9
The first statement will print 4 (since 8 / 2 is 4), and the second statement will print 9 (since 32 is 9).
Operators
Before we move on, let's take a look at the basic terminology. Operation signs such as + are called operators. They perform operations on certain values (operands). Operators are usually represented by one or more symbols, but occasionally, they can be represented by a word. Most of the operators are identical to those you'll have seen in math class.
console.log(8 + 2);
Here the + is an addition operator, 8 and 2 are operands.
The addition operation has two operands, positioned to the left and right of the operator +. Operations with two operands are called binary operations. If at least one operand is missing, for example, 3 + ; then the program will throw out a syntax error.
Besides binary operations (not operators) there are unary operations (with one operand) and even ternary (with three operands)! Moreover, operators may look the same but denote different operations.
console.log(-3); // => -3
In the example above, the unary operation applies to 3. An interpreter will read it as follows: the minus operator tells it to take the number 3 and find the opposite, which is -3.
You might be a bit confused since -3 is both a number and an operator with an operand, but that's simply how programming languages work.
Commutative operations
Do you remember the basic rule of arithmetic that "changing the order of the numbers we are adding doesn't change the sum"? It's called the commutative law.
A binary operation is commutative since swapping operands gets you the same result. Obviously, addition is a commutative operation: 3 + 2 = 2 + 3.
But is subtraction a commutative operation? Of course not: 2 - 3 ≠ 3 - 2. In programming, this law applies just like it does in arithmetic.
Moreover, most of the operations we face in real life are not commutative. Here is the conclusion: always pay attention to the order of things you work with.
Composition of operations
Suppose we want to calculate an expression such as 3 * 5 - 2. That is exactly how we would write it down:
console.log(3 * 5 - 2); // => 13
Note that the interpreter performs arithmetic operations in the right order: first division and multiplication, then addition and subtraction. Sometimes we want to change the order of calculations. We'll dig into this topic in the next lesson. Or another example:
console.log(2 * 4 * 5 * 10);
As you can see, we can combine operations, which allows us to compute even more complex compound expressions. To visualize how calculations are done inside the interpreter, let's look at an example: 2 * 4 * 5 * 10.
- First we calculate
2 * 4and get8 * 5 * 10 - Then we calculate
8 * 5, which gives us40 * 10 - Finally, do the last multiplication. The result will be
400
As a result, the interpreter joins complex compound expressions by performing the arithmetic operations contained in them sequentially, by default in the correct order: first multiplication and division, then addition and subtraction.
Operator precedence
Look closely at the expression 2 + 2 * 2 and try to work out the answer.
The correct answer is 6.
If you guessed 8, then you'll find this lesson useful. You'll have studied the order of operations in high school math. This concept defines the order in which operations are to be performed. For example, multiplication and division have a higher precedence than addition and subtraction, and exponentiation comes before all other arithmetic operations, e.g., 2 ** 3 * 2 gives us 16.
But sometimes we have to perform calculations in a non-standard order. In tricky cases, precedence can (and must) be set with parentheses, just like we did in high school, e.g., (2 + 2) * 2.
Parentheses fit with any operation. They can be nested into each other as many times as you need. Here are a couple of examples:
console.log(3 ** (4 - 2)); // => 9
console.log(7 * 3 + (4 / 2) - (8 + (2 - 1))); // => 14
Sometimes an expression may be visually cumbersome. In such cases, parentheses can come in handy without affecting the order of operations. For example, the task from the previous lesson becomes clearer with parentheses.
Before:
console.log(8 / 2 + 5 - -3 / 2); // => 10.5
After:
console.log(((8 / 2) + 5) - (-3 / 2)); // => 10.5
Note: code is written for humans, since they'll be the ones to read it, the machine just executes it. For the machine, code is either valid or invalid, it doesn't recognize "more" or "less" valid code.