JavaScript operators are used to perform operations on variables and values. Operators are used to assign values, compare values, perform arithmetic operations, and more. In this tutorial, you will learn about the different types of JavaScript operators and how to use them in your code.
Arithmetic operators are symbols that represent mathematical operations. They are used to perform calculations on numbers. In JavaScript, the arithmetic operators are used to perform mathematical operations on numbers.
Arithmetic operators are the most common and basic mathematical operators used in JavaScript. The following table lists all the arithmetic operators in JavaScript.
Operator | Description | Example |
---|---|---|
+ | Addition | 2 + 3 = 5 |
- | Subtraction | 5 - 2 = 3 |
* | Multiplication | 2 * 3 = 6 |
/ | Division | 6 / 2 = 3 |
% | Modulus | 5 % 2 = 1 |
++ | Increment | x++ |
-- | Decrement | x-- |
The addition operator (+) adds two numbers together and returns the result. The subtraction operator (-) subtracts one number from another and returns the result. The multiplication operator (*) multiplies two numbers together and returns the result. The division operator (/) divides one number by another and returns the result. The modulus operator (%) returns the remainder of a division operation. The increment operator (++) increments a number by one. The decrement operator (--) decrements a number by one.
Let's look at some examples of using the arithmetic operators in JavaScript.
let x = 2;
let y = 3;
let result = x + y;
console.log(result); // 5
In the example above, we have used the addition operator (+) to add two numbers together. The result is 5.
let x = 5;
let y = 2;
let result = x - y;
console.log(result); // 3
In the example above, we have used the subtraction operator (-) to subtract one number from another. The result is 3.
let x = 2;
let y = 3;
let result = x * y;
console.log(result); // 6
In the example above, we have used the multiplication operator (*) to multiply two numbers together. The result is 6.
let x = 6;
let y = 2;
let result = x / y;
console.log(result); // 3
In the example above, we have used the division operator (/) to divide one number by another. The result is 3.
let x = 5;
let y = 2;
let result = x % y;
console.log(result); // 1
In the example above, we have used the modulus operator (%) to return the remainder of a division operation. The result is 1.
let x = 2;
x++;
console.log(x); // 3
In the example above, we have used the increment operator (++) to increment a number by one. The result is 3.
let x = 2;
x--;
console.log(x); // 1
In the example above, we have used the decrement operator (--) to decrement a number by one. The result is 1.
Comparison operators are used to compare two values and return a boolean value (true or false). They are commonly used in conditional statements (if statements) and loops.
In JavaScript, there are six comparison operators. They are:
The following table shows an example of how each comparison operator works.
Operator | Example | Result |
---|---|---|
> | 2 > 1 | true |
< | 1 < 2 | true |
>= | 2 >= 2 | true |
<= | 2 <= 2 | true |
== | 2 == 2 | true |
!= | 2 != 1 | true |
The following code block demonstrates how comparison operators can be used in a conditional statement.
let a = 2;
if (a > 1) {
console.log('a is greater than 1');
} else {
console.log('a is not greater than 1');
}
In this example, the variable a
is assigned the value 2
. The if statement checks whether a
is greater than 1
. If it is, the statement console.log('a is greater than 1')
is executed. Otherwise, the statement console.log('a is not greater than 1')
is executed.
let x = 10;
let y = 20;
if (x === 10 && y === 20) {
console.log('x is 10 and y is 20');
}
let x = 10;
let y = 20;
if (x === 10 || y === 10) {
console.log('x is 10 or y is 10');
}
let x = 10;
if (!(x === 10)) {
console.log('x is not 10');
}
Operator | Name | Description |
---|---|---|
&& | AND | Returns true if both expressions are true. |
|| | OR | Returns true if one of the two expressions is true. |
! | NOT | Reverses the logical state of its operand. |
let x = 10;
let y = 20;
x = y;
console.log(x);
Operator | Example | Same as |
---|---|---|
+= | x += y | x = x + y |
-= | x -= y | x = x - y |
*= | x *= y | x = x * y |
/= | x /= y | x = x / y |
%= | x %= y | x = x % y |
**= | x **= y | x = x ** y |
let x = 10;
let y = 20;
x += y;
console.log(x);
let a = 4; // 0100
let b = 5; // 0101
let c = a & b; // 0100
console.log(c); // 4
let a = 4; // 0100
let b = 5; // 0101
let c = a | b; // 0101
console.log(c); // 5
condition ? result1 : result2
The condition is evaluated, and if it is true, the expression returns result1; otherwise, it returns result2. Here is an example of how to use the ternary operator: let age = 18;
let canDrink = age >= 21 ? 'Yes' : 'No';
console.log(canDrink);
let age = 18;
let canDrink = age >= 21 ? true : false;
console.log(canDrink);
let a = true;
let b = false;
let c = a ?? b;
console.log(c); // true
let d = b ?? a;
console.log(d); // true