Table of contents
Arithmetic Operators: Crunching Numbers with Ease
Addition (+): Combines two values, resulting in their sum.
5 + 3results in
"Hello" + "World"results in
Subtraction (-): Subtracts the RHS value from the LHS side value.
10 - 3equals
Multiplication (*): Multiplies two values, yielding a product.
4 * 6equals
Division (/): Divides the left-hand side value by the right-hand side value.
15 / 3equals
Modulus (%): Returns the remainder after dividing the LHS value by the RHS value.
17 % 5equals
Assignment Operators: Giving Values a Purpose
Assignment operators are all about giving values purpose and direction. They allow you to assign values to variables, making it easier to store and manipulate data. The simple and widely used assignment operator (=) enables you to assign a value to a variable. For instance,
let age = 15; assign the value
15 to the variable age.
Equal (=): Assigns a value to a variable.
x = 10assigns the value 10 to the variable x.
Add and Assign (+=): Adds a value to a variable and assigns the result.
x += 5is equivalent to
x = x + 5.
Subtract and Assign (-=): Subtracts a value from a variable and assigns the result.
x -= 3is equivalent to
x = x - 3.
Multiply and Assign (*=): Multiplies a variable by a value and assigns the result.
x *= 2is equivalent to
x = x * 2.
Divide and Assign (/=): Divides a variable by a value and assigns the result.
x /= 4is equivalent to
x = x / 4.
Modulus and Assign (%=): Calculates the modulus and assigns the result.
x %= 3is equivalent to
x = x % 3.
Comparison Operators: Making Sense of Differences
Comparison operators are your go-to tools for making sense of how different values relate to each other. These operators facilitate comparisons and return logical values, such as
false. Some of the essential comparison operators include:
Equal (==): Checks if two values are equal, regardless of their data type.
5 == 5evaluates to
Strict Equal (===): Compares both value and data type for equality.
5 === "5"evaluates to
Not Equal (!=): Determines if two values are not equal.
10 != 5evaluates to
Greater Than (>): Compare whether one value is larger than another.
8 > 3evaluates to
Less Than (<): Compare whether one value is smaller than another.
2 < 7evaluates to
Greater Than or Equal (>=): Check if the first value is greater than or equal to the second.
10 >= 10evaluates to
Less Than or Equal (<=): Check if the first value is less than or equal to the second.
5 <= 3evaluates to
Logical Operators: Unleashing the Power of Logic
Logical operators allow you to combine multiple conditions and evaluate complex expressions. These operators are vital for making decisions in your code. The three primary logical operators are:
AND (&&): Returns
trueif both conditions are true.
(x > 5) && (y < 10)evaluates to true only if
xis greater than
yis less than
OR (||): Returns
trueif at least one of the conditions is true.
(a > 10) || (b < 5)evaluates to true if either
ais greater than
bis less than
NOT (!): Flips the boolean value of a condition.
!(x > 3)evaluates to
xis not greater than
AND (&): Performs a bitwise AND operation between two numbers.
5 & 3results in
OR (|): Performs a bitwise OR operation between two numbers.
5 | 3results in
XOR (^): Performs a bitwise XOR operation between two numbers.
5 ^ 3results in
NOT (~): Performs a bitwise NOT operation on a single number.
Left Shift (<<): Shifts the bits of a number to the left.
4 << 2results in
Right Shift (>>): Shifts the bits of a number to the right.
16 >> 2results in
Unary Operators: Operating on a Single Operand
Unary operators are a unique set of operators that operate on a single operand. The increment (++) and decrement (--) operators are commonly used examples. They respectively increase or decrease the value of a variable by one.
Increment (++): Increments the value by one.
let a = 1; a++;results in
Decrement (--): Decrements the value by one.
let b = 2; --bresults in
Ternary Operator: A Concise Conditional Choice
The ternary operator is a concise way to make quick decisions in your code. It's a shorthand version of a
if-else statement. The syntax is as follows:
condition ? value_if_true : value_if_false.
Hope you like it, if yes ❤️ like & 📤share.
Thanks for your time.