Contents
Operators
Operators in JavaScript allow you to perform various operations on data, such as calculations, comparisons, logical operations, and assignments. They are a fundamental part of working with variables and values, enabling you to write dynamic and functional code.
Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations on numbers.
Operator | Description | Example | Output |
---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 10 - 4 | 6 |
* | Multiplication | 6 * 3 | 18 |
/ | Division | 15 / 3 | 5 |
% | Modulus (Remainder) | 17 % 5 | 2 |
++ | Increment (adds 1) | let x = 5; x++ | 6 |
-- | Decrement (subtracts 1) | let y = 5; y-- | 4 |
Examples:
let x = 10;
let y = 3;
console.log(x + y); // Outputs: 13
console.log(x - y); // Outputs: 7
console.log(x * y); // Outputs: 30
console.log(x / y); // Outputs: 3.3333333333333335
console.log(x % y); // Outputs: 1
console.log(x ** y); // Outputs: 1000
- Increment and Decrement: When used as
x++
orx--
, the value is changed after the expression is evaluated. When used as++x
or--x
, the value is changed before the expression is evaluated.
Comparison Operators
Comparison operators compare two values and return a Boolean (true
or false
). They are commonly used in conditional statements.
Operator | Description | Example | Output |
---|---|---|---|
== | Equal to | 5 == '5' | true |
=== | Strict equal to (type + value) | 5 === '5' | false |
!= | Not equal to | 5 != '6' | true |
!== | Strict not equal to | 5 !== '5' | true |
> | Greater than | 10 > 5 | true |
< | Less than | 3 < 7 | true |
>= | Greater than or equal to | 8 >= 8 | true |
<= | Less than or equal to | 4 <= 3 | false |
Examples:
let a = 5;
let b = '5';
console.log(a == b); // Outputs: true (equality operator)
console.log(a === b); // Outputs: false (strict equality operator)
console.log(a != b); // Outputs: false (inequality operator)
console.log(a !== b); // Outputs: true (strict inequality operator)
console.log(a > 3); // Outputs: true
console.log(a < 10); // Outputs: true
console.log(a >= 5); // Outputs: true
console.log(a <= 4); // Outputs: false
==
vs.===
:==
compares values but not types (loose equality), while===
compares both values and types (strict equality).
Logical Operators
Logical operators are used to combine multiple Boolean expressions or values and return a Boolean result.
Operator | Description | Example | Output |
---|---|---|---|
&& | Logical AND | true && false | false |
|| | Logical OR | true && false | true |
! | Logical NOT | !true | false |
Examples:
let isAdult = true;
let hasPermission = false;
let canEnter = isAdult && hasPermission; // false (both must be true)
let canView = isAdult || hasPermission; // true (at least one must be true)
let isDenied = !hasPermission; // true (negates the value)
&&
(AND): Returnstrue
if both operands aretrue
.||
(OR): Returnstrue
if at least one operand istrue
.!
(NOT): Returns the opposite Boolean value.
Assignment Operators
Assignment operators are used to assign values to variables. They also include shorthand operators for performing arithmetic operations and updating variables.
Operator | Description | Example | Equivalent to |
---|---|---|---|
= | Assignment | x = 10 | – |
+= | Addition assignment | x += 5 | x = x + 5 |
-= | Subtraction assignment | x -= 3 | x = x - 3 |
*= | Multiplication assignment | x *= 2 | x = x * 2 |
/= | Division assignment | x /= 4 | x = x / 4 |
%= | Modulus assignment | x %= 2 | x = x % 2 |
Examples:
let x = 10;
x += 5; // Equivalent to x = x + 5
console.log(x); // Outputs: 15
x -= 3; // Equivalent to x = x - 3
console.log(x); // Outputs: 12
x *= 2; // Equivalent to x = x * 2
console.log(x); // Outputs: 24
x /= 4; // Equivalent to x = x / 4
console.log(x); // Outputs: 6
x %= 3; // Equivalent to x = x % 3
console.log(x); // Outputs: 0
x = 2;
x **= 3; // Equivalent to x = x ** 3
console.log(x); // Outputs: 8
Conditional (Ternary) Operator
The ternary operator (condition ? expression1 : expression2
) is a shorthand way of writing an if-else
statement. It checks a condition and returns one of two values based on whether the condition is true
or false
.
Syntax:
let result = condition ? valueIfTrue : valueIfFalse;
Example:
let age = 20;
let access = age >= 18 ? "Allowed" : "Denied";
console.log(access); // Output: "Allowed"
In this example, the condition (age >= 18
) is evaluated. If it’s true
, "Allowed"
is assigned to access
; otherwise, "Denied"
is assigned.
Nested Ternary Example:
let score = 85;
let grade = score >= 90 ? 'A' : score >= 80 ? 'B' : 'C';
console.log(grade); // Output: "B"
While you can nest ternary operators, it’s best to avoid excessive nesting as it can reduce code readability. In such cases, using an if-else
statement is preferable.
Summary
JavaScript provides a variety of operators to perform arithmetic calculations, comparisons, logical operations, and value assignments. Arithmetic operators handle basic math, while comparison operators are used to evaluate conditions. Logical operators (&&
, ||
, !
) allow you to combine or invert Boolean expressions. Assignment operators simplify updating variable values, and the conditional (ternary) operator offers a concise way to write if-else
conditions. Understanding how to use these operators effectively is fundamental to writing functional and expressive JavaScript code.