Operators
Last updated
Last updated
Operators in JavaScript are used to perform operations on variables and values. They are essential tools for manipulating data and implementing logic in your programs. Let's explore the different types of operators available in JavaScript.
Arithmetic operators are used to perform basic mathematical operations.
Operator | Description | Example | Result |
---|---|---|---|
Example:
Assignment operators are used to assign values to variables.
Example:
Comparison operators are used to compare two values and return a boolean result (true
or false
).
Example:
Logical operators are used to combine multiple boolean expressions.
Example:
The concatenation operator (+
) is used to combine strings.
Example:
The conditional (ternary) operator is a shorthand for the if-else
statement. It takes three operands: a condition, an expression to execute if the condition is true, and an expression to execute if the condition is false.
Syntax:
Example:
Operators are essential tools for performing various operations on variables and values in JavaScript. By understanding arithmetic, assignment, comparison, logical, string, and conditional operators, you can effectively manipulate data and implement logic in your programs.
Operator | Description | Example | Equivalent to |
---|---|---|---|
Operator | Description | Example | Result |
---|---|---|---|
Operator | Description | Example | Result |
---|---|---|---|
+
Addition
5 + 2
7
-
Subtraction
5 - 2
3
*
Multiplication
5 * 2
10
/
Division
5 / 2
2.5
%
Modulus (Remainder)
5 % 2
1
++
Increment
let a = 5; a++
a
becomes 6
--
Decrement
let a = 5; a--
a
becomes 4
=
Assign
x = y
x = y
+=
Add and assign
x += y
x = x + y
-=
Subtract and assign
x -= y
x = x - y
*=
Multiply and assign
x *= y
x = x * y
/=
Divide and assign
x /= y
x = x / y
%=
Modulus and assign
x %= y
x = x % y
==
Equal to
5 == '5'
true
===
Strict equal to
5 === '5'
false
!=
Not equal to
5 != '5'
false
!==
Strict not equal to
5 !== '5'
true
>
Greater than
5 > 2
true
<
Less than
5 < 2
false
>=
Greater than or equal to
5 >= 5
true
<=
Less than or equal to
5 <= 5
true
&&
Logical AND
true && false
false
`
`
Logical OR
`true
!
Logical NOT
!true
false