Operators
Operators in JavaScript are used to perform operations on our variables and values. They are essential for performing arithmetic calculations, comparing values, assigning values, and more. Understanding how to use operators is crucial for writing effective JavaScript code.
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and more.
Addition (
+
): Adds two numbers.Subtraction (
-
): Subtracts one number from another.Multiplication (
*
): Multiplies two numbers.Division (
/
): Divides one number by another.Modulus (
%
): Returns the remainder of a division operation.Increment (
++
): Increases a number by one.Decrement (
-
): Decreases a number by one (same as subtraction, yes).
Examples:
2. Comparison Operators
Comparison operators are used to compare two values and return a boolean value (true
or false
). They are commonly used in conditional statements to perform different actions based on different conditions.
Equal to (
==
): Compares two values for equality, ignoring data type.Strict equal to (
===
): Compares two values for equality, considering data type.Not equal to (
!=
): Compares two values for inequality, ignoring data type.Strict not equal to (
!==
): Compares two values for inequality, considering data type.Greater than (
>
): Checks if the left value is greater than the right value.Less than (
<
): Checks if the left value is less than the right value.Greater than or equal to (
>=
): Checks if the left value is greater than or equal to the right value.Less than or equal to (
<=
): Checks if the left value is less than or equal to the right value.
Examples:
3. Logical Operators
Logical operators are used to combine multiple conditions. They are often used in control flow statements to create more complex conditions (more info).
Logical AND (
&&
): Returnstrue
if both conditions are true.Logical OR (
||
): Returnstrue
if at least one condition is true.Logical NOT (
!
): Returns the opposite boolean value of the condition.
Examples:
4. Assignment Operators
Assignment operators are used to assign values to variables. They include the basic assignment operator as well as compound assignment operators that combine arithmetic operations with assignment.
Assignment (
=
): Assigns a value to a variable.Addition assignment (
+=
): Adds a value to a variable and assigns the result to the variable.Subtraction assignment (
=
): Subtracts a value from a variable and assigns the result to the variable.Multiplication assignment (
=
): Multiplies a variable by a value and assigns the result to the variable.Division assignment (
/=
): Divides a variable by a value and assigns the result to the variable.Modulus assignment (
%=
): Takes the modulus of a variable by a value and assigns the result to the variable.
Examples:
5. Ternary Operator
The ternary operator is a shorthand for the if
statement (next chapter). It allows you to perform a simple conditional assignment or operation.
Ternary operator (
condition ? expr1 : expr2
): Evaluates a condition and returnsexpr1
if the condition is true, andexpr2
if the condition is false.
Example:
Use Case:
Suppose you're building a simple application to check if a user is eligible to vote based on their age. You might use various operators to perform this check and display the result.
Example:
In this example:
We declare a variable
userAge
to store the age of the user.We use the ternary operator to check if the user is 18 or older and assign the result to the
isEligibleToVote
variable.We use
console.log
to print whether the user is eligible to vote.
This use case demonstrates how different operators can be used to perform checks and make decisions in your code. Understanding these operators is essential for building logical and functional JavaScript programs.
Help us improve the content 🤩
You can leave comments here.
Last updated