Operators
Operators
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.
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations.
+
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
Example:
2. Assignment Operators
Assignment operators are used to assign values to variables.
=
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
Example:
3. Comparison Operators
Comparison operators are used to compare two values and return a boolean result (true
or false
).
==
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
Example:
4. Logical Operators
Logical operators are used to combine multiple boolean expressions.
&&
Logical AND
true && false
false
`
`
Logical OR
`true
!
Logical NOT
!true
false
Example:
5. String Operators
The concatenation operator (+
) is used to combine strings.
Example:
6. Conditional (Ternary) Operator
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:
Summary
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.
Last updated