# Operators in JavaScript: Definition & Examples

Instructor: Vignesh Sivabalan
Operators are essential in a programming language for performing various operations including arithmetic, logical, bitwise, conditional, and so on. This lesson explains various JavaScript Operators with clear examples.

## Introduction to JavaScript Operators

The Operators in JavaScript are helpful in comparing values, assigning values, performing arithmetic and logical operations, and so on. Let us consider a very simple expression 3 * 7 = 21. Here, 3 and 7 are ''operands'' and ''*'' is an operator. The following operators are supported by JavaScript:

1. Arithmetic Operators
2. Logical Operators
3. Comparison Operators
4. Conditional Operators
5. Assignment Operators
6. String Operators
7. Bitwise Operators
8. typeof Operator
9. delete Operator
10. in Operator
11. instanceof Operator

## Arithmetic Operators

JavaScript provides support for the following arithmetic operators:

Let us consider two operands (variables) x and y.

Arithmetic Operation Arithmetic Operator Example Result
Addition + x + y If x = 7 and y = 8, then x + y = 15.
Subtraction - x - y If x = 8 and y = 6, then x - y = 2.
Multiplication * x * y If x = 3 and y = 3, then x * y = 9.
Division / x / y If x = 10 and y = 5, then x / y = 2.

## Logical Operators

JavaScript provides support for the following Logical or Relational Operators. Let us assume two variables x = 5 and y = 10, then:

Logical Operation Logical Operator Example Result Explanation
Logical AND && if (x > 3 && y > 9) TRUE AND returns TRUE if both the conditions are TRUE. Here, 5 > 3 and 10 > 9 are both TRUE and hence result of the AND operation is ''TRUE''.
Logical OR || if(x > 7||y > 8) TRUE OR returns TRUE even if one of the conditions is TRUE. Here, 5 > 7 is false but 10 > 8 is true and hence the result is ''TRUE''.
Logical NOT ! !(x && y) FALSE ''!'' reverses the logical result of AND operation. The result of AND operation is ''TRUE'' and hence the result of NOT operator is ''FALSE''.

## Comparison Operators

The Comparison Operators are used to compare two or more values. The following comparison operators are supported by JavaScript.

Comparison Operator Explanation Example
Equal (==) Checks if the values of two operands are equal. If they are equal, it returns ''TRUE'' else ''FALSE'' If x = 6 and y = 8, then (x == y) is FALSE.
Not Equal to (!=) Checks if the values of two operands are not equal. If they are not equal, it returns ''TRUE'' else ''FALSE'' If x = 6 and y = 8, then (x != y) is TRUE
Greater than (>) Checks if the value of left operand is greater than right operand value. If the condition holds true, it returns ''TRUE'' else ''FALSE'' If x = 6 and y = 8, then (x > y) is FALSE.
Less than (<) Checks if the value of left operand is lesser than the right operand value. If the condition holds true, it returns ''TRUE'' else ''FALSE'' If x = 6 and y = 8, then (x < y) is TRUE.
Greater than or Equal to (>=) Checks if the values of two operands are equal or if the left operand is greater than the right. If either is true, it returns ''TRUE'' else ''FALSE'' If x = 8 and y = 6, then (x >= y) is TRUE.
Less than or Equal to (<=) Checks if the value of left operand is less than or equal to the value of right operand. If the condition is true, it returns ''TRUE'' else ''FALSE'' If x = 6 and y = 6, then (x <= y) is TRUE.

## Conditional Operator

Based on certain conditions, the conditional or ternary operator assigns values to the variables.

Syntax:

`variable_name = (conditions) ? val1 : val2`

Example:

`employable = (age > 18) ? ''Can be employed'' : ''Too young to be employed''`

## Assignment Operators

Comparison Operator Example Explanation Result
Simple Assignment (=) x = y + z Adds the value of y with z and assigns the value to x. For example: If z = 3 and y = 2, then x = 5.
Add and Assign (+=) x += y x += y is equivalent to x = x + y. For example: If x = 3 and y = 2, then x += y is 5.
Subtract and Assign (-=) x -= y x -=y is equivalent to x = x - y. For example: If x = 3 and y = 2, then x -= y is 1.
Multiply and Assign (*=) x *= y x *= y is same as x = x * y. For example: If x = 3 and y = 2, then x *= y is 6.
Divide and Assign (/=) x /= y x /= y is equivalent to x = x / y. For example: If x = 3 and y = 2, then x /= y is 1.5.
Modulo and Assign (%=) x %= y x %= y is x = x % y; Modulo divides the values of y with x and assigns the remainder to x. For example: If x = 3 and y = 2, then x %= y is 1.

## String Operators

The string operators are + and += operators that concatenate strings.

Example: text_one = ''good'' and text_two = ''boy'' then text_one + text_two = ''good boy''

## Bitwise Operators

These operators work with 32 bit numericals. Any number is first converted to 32 bits for operation and the result is again converted to a decimal number.

To unlock this lesson you must be a Study.com Member.

### Register to view this lesson

Are you a student or a teacher?

#### See for yourself why 30 million people use Study.com

##### Become a Study.com member and start learning now.
Back
What teachers are saying about Study.com

### Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.