JavaScript operators are fundamental building blocks for any programming language. They allow us to perform operations on variables and values. From simple arithmetic to complex logical decisions, understanding operators is crucial for writing effective JavaScript code.
Arithmetic Operators
These are used for mathematical calculations:
+
(Addition): Adds two operands. 5 + 3
results in 8
.
-
(Subtraction): Subtracts the second operand from the first. 10 - 4
results in 6
.
*
(Multiplication): Multiplies two operands. 6 * 7
results in 42
.
/
(Division): Divides the first operand by the second. 15 / 3
results in 5
.
%
(Modulo/Remainder): Returns the remainder of a division. 10 % 3
results in 1
.
++
(Increment): Increases the value of a variable by 1. let count = 5; count++;
makes count
equal to 6
.
--
(Decrement): Decreases the value of a variable by 1. let count = 5; count--;
makes count
equal to 4
.
Assignment Operators
Used to assign values to variables:
=
(Assignment): Assigns the value of the right operand to the left operand. let x = 10;
+=
(Addition Assignment): Adds the right operand to the left operand and assigns the result to the left operand. x += 5;
is equivalent to x = x + 5;
-=
(Subtraction Assignment): Subtracts the right operand from the left operand and assigns the result. x -= 3;
is equivalent to x = x - 3;
*=
(Multiplication Assignment): Multiplies the left operand by the right operand and assigns the result. x *= 2;
is equivalent to x = x * 2;
/=
(Division Assignment): Divides the left operand by the right operand and assigns the result. x /= 4;
is equivalent to x = x / 4;
%=
(Modulo Assignment): Returns the remainder of the division and assigns it to the left operand. x %= 3;
is equivalent to x = x % 3;
Comparison Operators
Used to compare two values:
==
(Equality): Compares two values for equality, performing type coercion if necessary. 5 == '5'
is true
.
!=
(Inequality): Compares two values for inequality, performing type coercion. 5 != '10'
is true
.
===
(Strict Equality): Compares two values for equality without type coercion. 5 === '5'
is false
.
!==
(Strict Inequality): Compares two values for inequality without type coercion. 5 !== '5'
is true
.
>
(Greater Than): Checks if the left operand is greater than the right. 10 > 5
is true
.
<
(Less Than): Checks if the left operand is less than the right. 5 < 10
is true
.
>=
(Greater Than or Equal To): Checks if the left operand is greater than or equal to the right. 10 >= 10
is true
.
<=
(Less Than or Equal To): Checks if the left operand is less than or equal to the right. 5 <= 5
is true
.
Logical Operators
Used to combine conditional statements:
&&
(Logical AND): Returns true
if both operands are true. (true && false)
is false
.
||
(Logical OR): Returns true
if at least one operand is true. (true || false)
is true
.
!
(Logical NOT): Reverses the boolean value of the operand. !true
is false
.
Unary Operators
Operators that operate on a single operand:
typeof
: Returns the type of a variable. typeof 42
returns "number"
.
delete
: Removes a property from an object. delete obj.property;
void
: Evaluates an expression and returns undefined
.
Other Important Operators
? :
(Ternary Operator): A shorthand for an if-else statement. condition ? value_if_true : value_if_false
. Example: let age = 20; let status = age >= 18 ? "Adult" : "Minor";
instanceof
: Checks if an object is an instance of a particular class or constructor. myArray instanceof Array
returns true
.
in
: Checks if a property exists in an object. 'name' in myObject
returns true
if myObject
has a property named 'name'
.
Understanding and using these operators effectively will significantly enhance your JavaScript programming skills. Practice with different examples to solidify your knowledge!