JavaScript Operators
Operators in JavaScript are symbols or keywords that perform operations on one or more operands. They are instrumental in manipulating values and variables within expressions. Let's explore some of the key operators in JavaScript.
Type | Operator | Description | Example |
---|---|---|---|
Arithmetic | + | Addition | 2 + 3 = 5 |
- | Subtraction | 5 - 2 = 3 | |
* | Multiplication | 2 * 4 = 8 | |
/ | Division | 10 / 2 = 5 | |
% | Modulus (remainder) | 8 % 3 = 2 | |
** | Exponentiation | 2 ** 3 = 8 | |
Assignment | = | Assignment | x = 10 |
+= | Add and assign | x += 5 (equivalent to x = x + 5 ) | |
-= | Subtract and assign | y -= 3 (equivalent to y = y - 3 ) | |
*= | Multiply and assign | z *= 2 (equivalent to z = z * 2 ) | |
/= | Divide and assign | a /= 4 (equivalent to a = a / 4 ) | |
%= | Modulus and assign | b %= 7 (equivalent to b = b % 7 ) | |
Comparison | == | Loose equality (checks value, not type) | 10 == "10" (true) |
=== | Strict equality (checks both value and type) | 10 === "10" (false) | |
!= | Loose inequality | 5 != 7 (true) | |
!== | Strict inequality | "5" !== 7 (true) | |
< | Less than | 2 < 5 (true) | |
> | Greater than | 8 > 3 (true) | |
<= | Less than or equal to | 4 <= 4 (true) | |
>= | Greater than or equal to | 1 >= 0 (true) | |
Logical | && | Logical AND (both operands must be truthy) | true && false = false |
` | ` | ||
! | Logical NOT (inverts the operand) | !true = false | |
Increment/Decrement | ++ | Increment (adds 1) | x++ (increment after) or ++x (increment before) |
-- | Decrement (subtracts 1) | y-- (decrement after) or --y (decrement before) | |
Ternary | ? : | Conditional expression | age >= 18 ? "adult" : "minor" |
Typeof | typeof | Returns the type of a value | typeof 10 = "number" |
In | in | Checks if a property exists in an object | "name" in person |
Delete | delete | Deletes a property from an object | delete person.age |
Arithmetic Operators:
Arithmetic operators perform basic mathematical operations.
-
Addition (
+
):let sum = 5 + 3; // Result: 8
-
Subtraction (
-
):let difference = 10 - 4; // Result: 6
-
Multiplication (
*
):let product = 7 * 2; // Result: 14
-
Division (
/
):let quotient = 20 / 5; // Result: 4
-
Modulus (
%
):let remainder = 15 % 4; // Result: 3
Assignment Operator (=
):
The assignment operator assigns a value to a variable.
let age = 25;
Comparison Operators:
Comparison operators are used to compare values, resulting in a Boolean outcome (true
or false
).
-
Equal to (
==
):let isEqual = 10 == 10; // Result: true
-
Strictly equal to (
===
):let isStrictlyEqual = "5" === 5; // Result: false
-
Not equal to (
!=
):let isNotEqual = 8 != 12; // Result: true
-
Greater than (
>
):let isGreater = 15 > 10; // Result: true
-
Less than (
<
):let isLess = 5 < 2; // Result: false
Logical Operators:
Logical operators perform logical operations and return Boolean values.
-
Logical AND (
&&
):let logicalAnd = true && false; // Result: false
-
Logical OR (
||
):let logicalOr = true || false; // Result: true
-
Logical NOT (
!
):let logicalNot = !true; // Result: false
Increment (++
) and Decrement (--
) Operators:
Increment and decrement operators are used to increase or decrease the value of a variable by 1.
let count = 5;
count++; // Increment: count is now 6
count--; // Decrement: count is now 5
Concatenation Operator (+
):
The +
operator is also used for string concatenation.
let greeting = "Hello, " + "World!"; // Result: "Hello, World!"
These are some fundamental operators in JavaScript. Understanding how to use them is essential for writing effective and expressive code. In upcoming chapters, we'll delve into more advanced operators and their applications.