|Addition||+||5 + 3||8|
|Subtraction||-||5 - 3||2|
|Multiplication||*||5 * 3||15|
|Division||/||5 / 3||1.67|
|Modulus||%||5 % 3||2|
// Declare two variables and assign them values var a = 5; var b = 3; // Perform arithmetic operations on the variables console.log(a + b); // 8 console.log(a - b); // 2 console.log(a * b); // 15 console.log(a / b); // 1.67 console.log(a % b); // 2
In the example above, we declared two variables
b, and assigned them the values of 5 and 3, respectively. We then used the arithmetic operators to perform various mathematical operations on the variables and logged the results to the console.
Assignment operators can be used to assign a value to a variable. The most commonly used assignment operator is the
= operator, which assigns the value on the right side of the operator to the variable on the left side.
// Declare a variable and assign it a value var x = 5; // Re-assign a new value to the variable x = 10; console.log(x); // 10
In the example above, we first declared a variable
x and assigned it the value of 5. We then re-assigned a new value of 10 to the variable and logged the new value to the console.
Other assignment operators include
/=, which perform the corresponding arithmetic operation on the variable and then assign the result to the variable.
// Declare a variable and assign it a value var x = 5; // Add 5 to the variable and re-assign the result x += 5; console.log(x); // 10
We used the
+= operator to add 5 to the value of x and re-assign the result of 10 to the variable.
Comparison operators can be used to compare two values and return a Boolean value of
|Equal||==||5 == 3|
|Not equal||!=||5 != 3|
|Strict equal||===||5 === "5"`|
|Strict not equal||!==||5 !== "5"|
|Greater than||>||5 > 3|
|Less than||<||5 < 3|
|Greater than or equal to||>=||5 >= 3|
|Less than or equal to||<=||5 <= 3|
Below we cover some basic examples:
// Declare two variables and assign them values var a = 5; var b = 3; // Compare the variables using comparison operators console.log(a == b); // false console.log(a != b); // true console.log(a === b); // false console.log(a !== b); // true console.log(a > b); // true console.log(a < b); // false console.log(a >= b); // true console.log(a <= b); // false
We declared two variables
b and assigned them the values of 5 and 3, respectively. We then used the comparison operators to compare the variables and logged the results to the console.
Note that the
!= operators perform type coercion, meaning they will attempt to convert the values to the same type before making the comparison. On the other hand,
!== operators perform strict equality comparison, which means that the values must be of the same type and value.
&&) and logical OR (
||) operators have a feature called short-circuiting. This means that if the outcome of the operation is already known, the second operand will not be evaluated. In the case of logical AND, if the first operand is "falsy", then the second operand will not be evaluated and the first operand will be returned. Similarly, in the case of logical OR, if the first operand is "truthy", then the second operand will not be evaluated and the first operand will be returned.
Arithmetic operators are used to performing mathematical operations on numbers.
Assignment operators are used to assigning a value to a variable.
Comparison operators are used to comparing two values and return a Boolean value of true or false.
Logical operators are used to performing logical operations on Boolean values.
instanceof, are used to check the type of a value or object. Bitwise operators, such as
~, perform operations on the individual bits of a value. We will cover these operators in more depth in future articles.