4 Types of JavaScript Operators You Must Know About

JavaScript relies on operators to execute a wide range of operations on data. This guide aims to provide you with an in-depth understanding of these operators and their diverse categories.

Let’s start!

What are Operators?

Operators are symbols that are used to perform operations on operands which can be variables or values. They play a pivotal role in achieving various tasks within your code.

Types of Operators

JavaScript provides the following types of operators.

1.Arithmetic Operators

If you want to perform mathematical calculations on numeric values, then you can use the arithmetic operators.

JavaScript provides the following arithmetic operators:

Addition (+)

The addition operator is used to add two numbers together.

For example:

Subtraction (-)

The subtraction operator is used to subtract two numbers.

For example:

Multiplication (*)

The multiplication operator is used to multiply two numbers.

For example:

Division (/)

The division operator is used to divide the first number by the second number.

For example:

Modulus (%)

The modulus operator returns the remainder of a division operation.

For example:

Increment (++)

The increment operator is used to increase the value of a variable by one.

For example:

Decrement (- -)

The decrement operator is used to decrease the value of a variable by one.

For example:

2.Assignment Operators

If you want to assign values to variables, then you can use the assignment operators. They can also perform operations while assigning values.

JavaScript provides the following assignment operators:

= operator

The assignment operator assigns a value to a variable.

For example:

+= operator

-= operator

*= operator

/= operator

%= operator

3.Comparison Operators

If you want to compare two variables, then you can use the comparison operators. These operators return a Boolean result (true or false) indicating whether the comparison is true or false.

JavaScript provides the following comparison operators:

Equality (==)

The equality operator is used to compare the values of variables.

For example:

In the above example, the output is true because the value of both variables is the same i.e. 5.

Inequality (!=)

The inequality operator is used to check if the values of two variables are not equal.

For example:

In the above example, the output is true because the value of x is not equal to the value of y.

Strict Equality (===)

The strict equality operator is used to compare both values and data types of variables.

For example:

In the above example, the output is false because the values of both variables are the same but their data types are not the same.

Strict Inequality (!==)

The strict inequality operator is used to check if the values and data types of two variables are not equal.

For example:

In the above example, the output is true because the data types of both variables are not the same.

Ternary Operator (Conditional Operator)

The ternary operator, also known as the conditional operator, allows you to write concise conditional statements.

The ternary operator has the following syntax:

Here, the condition is evaluated first. If the condition is true, then the expression before the : (colon) is executed, and its result becomes the result of the entire expression.

If the condition is false, then the expression after the : (colon) is executed, and its result becomes the result of the entire expression.

Example: 1

Example: 2

Nested Ternary Operators

A nested ternary operator is a conditional operator that is nested inside another conditional operator.

For example:

Here, we have a score variable with a value of 75.

In the ternary operator, the first condition checks if the score is greater than or equal to 90, if it’s true then it sets the value of the variable grade to “A” and if it’s false then it checks on the second condition which is score >= 70.

If the condition score >=70 is true then it sets the value of the variable grade to “B” and if it’s false then it checks on the next condition which is score >= 50.

If the condition score>=50 is true then it sets the value of the variable grade to “C” and if it’s false then it sets the value of the variable grade to “D”.

In this case, we have score = 75, which means score>=70, so the value of the variable grade is set to “B”.

Other Comparison Operators (<, >, <=, >=)

JavaScript also provides operators such as greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=).

Example: 1

Example: 2

Example: 3

Example: 4

4.Logical Operators

Logical operators are used on one or more variables and return true or false.

JavaScript provides the following logical operators:

Logical AND (&&)

The logical AND operator returns true if both of its operands are true. Otherwise, it returns false. It can be used to check multiple conditions.

Example: 1

In the above example, the output is true because condition x<15 is true and condition x>12 is true i.e., both conditions are true.

Example: 2

In the above example, the output is false because condition x<15 is true, but condition x>14 is false i.e., both conditions are not true.

You can also check more than two conditions using the && operator. For example:

Here the output is false because one variable (c) is false.

Logical OR (||)

The logical OR operator returns true if at least one of its operands is true. It returns false if both operands are false.

Example:1

Here the output is true because one of the conditions (x<20) is true.

Example: 2

Here the output is false because both of the conditions are false.

Example: 3

Here the output is true because one of the conditions is true.

Logical NOT (!)

The logical NOT operator reverses a Boolean value. That means if the operand is true, it becomes false, and if the operand is false, it becomes true.

For example:

Conclusion

In this comprehensive guide, we’ve learned about JavaScript operators, which are essential for performing a wide range of operations on data. Here’s a quick recap of what we’ve covered:

  1. Arithmetic Operators: These operators are used for mathematical calculations, including addition, subtraction, multiplication, division, modulus, increment, and decrement.
  2. Assignment Operators: These are used to assign values to variables and can also perform operations while assigning values.
    Assignment operators include =+=-=*=/=%=.
  3. Comparison Operators: These operators are used to compare values and return true or false based on the comparison.
    Comparison operators include equality (==), inequality (!=), strict equality (===), strict inequality (!==), ternary operator(? :), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=).
  4. Logical Operators: These operators are used to perform logical operations on Boolean values.
    Logical operators include logical AND (&&), logical OR (||), and logical NOT (!).

Throughout the guide, I’ve provided detailed explanations and examples for each type of operator.

Thanks for reading.

For more content like this click here.

Check out toast.log, a browser extension that lets you see errors, warnings, and logs as they happen on your site — without having to open the browser’s console. Click here to get a 25% discount on toast.log.

Keep Coding!

Leave a Comment