Operators¶
JavaScript operators allow us to perform tests, do math, join strings together, and other such things.
First let's look at arithmetic operators, for example:
Addition console.log(6 + 9);
Subtraction console.log(20 - 15);
Multiplication console.log(3 * 7);
Division console.log(10 / 5);
You can also use the + operator to join text strings together (in programming, this is called concatenation).
1 2 3 4 5 6 |
|
There are also some shortcut operators available, called augmented assignment operators. For example, if you want to simply add a new text string to an existing one and return the result, you could do this:
1 2 3 |
|
When we are running true/false tests (for example inside conditionals — see next lesson) we use comparison operators. For example:
===
Tests strict equality (is it exactly the same?)
1 2 3 4 |
|
!==
Tests non-equality (is it not the same?)
1 2 3 4 |
|
<
Tests less than, <=
less than or equal
1 2 3 4 |
|
>
Tests greater than, >=
greater than or equal
1 2 3 4 |
|