The Wayback Machine - https://web.archive.org/web/20240710083344/https://www.geeksforgeeks.org/javascript-operators/
Open In App

JavaScript Operators

Last Updated : 04 Jul, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript Operators are symbols used to perform specific mathematical, comparison, assignment, and logical computations on operands. They are fundamental elements in JavaScript programming, allowing developers to manipulate data and control program flow efficiently. Understanding the different types of operators and how they work is important for writing effective and optimized JavaScript code.

Image

JavaScript Operators

There are various operators supported by JavaScript.

JavaScript Arithmetic Operators

JavaScript Arithmetic Operators perform arithmetic operations: addition (+), subtraction (-), multiplication (*), division (/), modulus (%), and exponentiation (**).

Name

Description

Syntax

Example

Addition (+)

Addition ‘+’ operator performs addition on two operands. This ‘+’ operator can also be used to concatenate (add) strings.

Y = “Geeks” + “for” + “Geeks” gives Y = “GeeksforGeeks” Y

Try

Subtraction (-)

Subtraction ‘-‘ operator performs subtraction on two operands.

Y = 5 – 3 gives Y = 2

Multiplication (*)

Multiplication ‘*’ operator performs multiplication on two operands.

Y = 5 * 5 gives Y = 25

Division (/) 

Division ‘/’ operator performs division on two operands (divide the numerator by the denominator).

Y = 5 / 5 gives Y = 1

Modulus (%)

Modulus ‘%’ operator gives a remainder of an integer division. 

A % B means remainder (A/B) Y = 5 % 4 gives Y = 1

Exponentiation (**)

Exponentiation ‘**’ operator give the power of the first operator raised to the second operator.

Y = 5 ** 3 gives Y = 125

Increment(++)

Increment ‘+ +’ operator increases an integer value by one. 

let A = 10 and Y = A + + then A = 11, Y=10

Decrement (–)

Decrement ‘- -‘ operator decreases an integer value by one.

let A = 10 and Y = A – – then A = 9, Y=10

Unary (+)

Unary ‘+’ is the fastest and preferred way of converting something into a number

+a means a is a positive number

Negation (-)

Negation ‘-‘ operator gives the negation of an operand.

-a means a is a negative number

JavaScript Assignment Operators

The assignment operation evaluates the assigned value. Chaining the assignment operator is possible in order to assign a single value to multiple variables

Name

Description

Syntax

Example

Assignment (=)

This operator assigns the right operand value to the left operand.

If A = 10 and Y = A then Y = 10

Addition Assignment (+=)

Sums up left and right operand values and then assigns the result to the left operand.

Y += 1 gives Y = Y + 1

Subtraction Assignment (-=)

It subtracts the right side value from the left side value and then assigns the result to the left operand. 

Y -= 1 gives Y = Y – 1

Multiplication Assignment (*=)

It multiplies a variable by the value of the right operand and assigns the result to the variable.

Y *= A is equivalent to Y = Y * A

Division Assignment (/=)

It divides a variable by the value of the right operand and assigns the result to the variable.

Y /= A is equivalent to Y = Y / A

Modules/Remainder Assignment (%=)

It divides a variable by the value of the right operand and assigns the remainder to the variable.

Y %= A is equivalent to Y = Y % A

Exponentiation Assignment (**=)

This raises the value of a variable to the power of the right operand.

Y **= A is equivalent to Y=Y ** A

Left Shift Assignment (<<=)

It moves the specified amount of bits to the left and assigns the result to the variable.

Y <<= A is equivalent to Y=Y << A

Right Shift Assignment (>>=)

It moves the specified amount of bits to the right and assigns the result to the variable.

Y >>= A is equivalent to Y = Y >> A

Bitwise AND Assignment (&=)

: It does a bitwise AND operation on the operand, and assigns the result to the variable.

Y &= b is equivalent to Y = Y & A

Bitwise OR Assignment (|=)

It does a bitwise OR operation on the operand, and assigns the result to the variable.

Y |= A is equivalent to Y= Y | b

Bitwise XOR Assignment (^=)

It does a bitwise XOR operation on the operand, and assigns the result to the variable.

Y ^= A is equivalent to Y= Y ^ A

JavaScript Comparison Operators

Comparison operators are mainly used to perform the logical operations that determine the equality or difference between the values.

Name

Description

Syntax

Example

Equality (==)

Compares the equality of two operands. If equal then the condition is true otherwise false.

Y = 5 and X = 6 Y = = X is false.

Strict equality (===)

Compares the equality of two operands with type. If both value and type are equal then the condition is true otherwise false.

Y = 5 and X = ‘5’ Y = = = X is false.

Inequality (!=)

Compares inequality of two operands. True if operands are not equal. 

let X = 10 then X ! = 11 is true.

Strict inequality (!==)

Compares the inequality of two operands with type. If both value and type are equal then the condition is true otherwise false.

let X = 10 then X ! == ’10’ is true.

Greater than (>)

This operator checks whether the left side value is greater than the right side value. If yes then it returns true otherwise it returns false. 

let X = 10 then X > 11 is false.

Less than (<)

This operator checks whether the left side value is less than the right side value. If yes then it returns true otherwise it returns false. 

let X = 10 then X < 11 is true.

Greater than or Equal to (>=)

This operator checks whether the left side operand is greater than or equal to the right side operand. If yes then it returns true otherwise it returns false. 

let X = 10 then X > = 11 is false.

Less than or Equal to (<= )

This operator checks whether the left side operand value is less than or equal to the right side operand value. If yes then it returns true otherwise it returns false. 

let X = 10 then X < = 10 is true.

JavaScript Logical Operators

JavaScript Logical Operators perform logical operations: AND (&&), OR (||), and NOT (!), evaluating expressions and returning boolean values.

Name

Description

Syntax

Example

Logical AND (&&)

It checks whether two operands are non-zero (0, false, undefined, null, or “” are considered as zero), if yes then return the last operand when evaluating from left to right

Y = 5 and X = 6 Y && X is 6.

Logical OR (||)

It checks whether two operands are non-zero (0, false, undefined, null, or “” is considered as zero), if yes then return the first operand when evaluating from left to right. 

Y = 5 and X = 0 Y || X is 5.

Logical NOT (!) 

It reverses the boolean result of the operand (or condition).

Y = 5 and X = 0 !(Y || X) is false.

JavaScript Bitwise Operators

The bitwise operator in JavaScript is used to convert the number to a 32-bit binary number and perform the bitwise operation. The number is converted back to the 64-bit number after the result. 

Name

Description

Syntax

Example

Bitwise AND (&)

The operator returns true only if both the operands are true

A = 6, B=1 A&B = 0

Bitwise OR (|)

The operator returns true even if one of the operands is true 

A = 6, B=1 A|B = 7

Bitwise XOR (^)

The operator returns true if both operators are distinct

A = 6, B=1 A^B = 7

Bitwise NOT (~)

This operator is used to invert the boolean value of the operand

A = 6 ~A = -7

Bitwise Left Shift (<<)

In this two operators are used where the first operand is the number and the second operand is the number of bits to shift to the left.

A = 6, B=1 A<<B = 12

Bitwise Right Shift (>>)

In this two operators are used where the first operand is the number and the second operand is the number of bits to shift to the right.

A = 6, B=1 A>>B = 3

Zero Fill Right Shift (>>>)

It is same as a bitwise right shift the only difference is that overflowing bits are discarded.

A = 6, B=1 A>>>B = 3

JavaScript Ternary Operators

The ternary operator has three operands. It is the simplified operator of if/else.

Name

Description

Syntax

Example

Ternary Operator (?:)

It is like the short form of the if-else condition. 

Y = ? A : B If the condition is true then Y = A otherwise Y = B

JavaScript Comma Operators

Comma Operator (,) mainly evaluates its operands from left to right sequentially and returns the value of the rightmost operand. 

Name

Description

Syntax

Example

 comma operator (,)

When a comma operator is placed in an expression, it executes each expression and returns the rightmost expression.

Expression1, Expression2, Expression3, …so on

JavaScript Unary Operators

A unary operation is an operation with only one operand.

Name

Description

Syntax

Example

JavaScript typeof

It returns the operand type, The possible types that exist in javascript are undefined, Object, boolean, number, string, symbol, and function.

typeof variable

Delete

This operator is more specifically used to delete JavaScript object properties.

delete object

// or

delete object.property

// or

delete object[‘property’]

JavaScript Relational Operators

JavaScript Relational operators are used to compare its operands and determine the relationship between them. They return a Boolean value (true or false) based on the comparison result.

Name

Description

Syntax

Example

in

The in operator returns true if the specified property is in the specified object.

propNameOrNumber in objectName

instanceof

The instanceof operator returns true if the specified object is of the specified object type.

objectName instanceof objectType

JavaScript BigInt Operators

JavaScript BigInt operators support arithmetic operations on BigInt data type, including addition, subtraction, multiplication, division, and exponentiation. Most operators that can be used between numbers can be used between BigInt values as well.

Name

Description

Syntax

Example

BigInt

It returns a new BigInt object that represents the original value.

BigInt(value);

JavaScript String Operators

JavaScript String Operators include concatenation (+) and concatenation assignment (+=), used to join strings or combine strings with other data types.

Name

Description

Syntax

Example

 concatenation operator (+)

It concatenates two string values together, returning another string that is the union of the two operand strings.

str1 + str2

Conclusion

JavaScript operators are essential tools that empower developers to perform a wide array of operations on data, from simple arithmetic to complex logical decisions. By mastering these operators, you can write more efficient, readable, and maintainable code. Whether you’re performing calculations, making comparisons, assigning values, or manipulating data, understanding how to use JavaScript operators effectively is a fundamental skill for any JavaScript developer. As you continue to build your programming expertise, the versatility and power of operators will become increasingly apparent, enabling you to create more dynamic and robust applications.

JavaScript Operators – FAQs

What are JavaScript operators?

JavaScript operators are special symbols used to perform operations on operands, which can be values or variables. They allow you to manipulate data and perform computations.

What are the different types of JavaScript operators?

The main types of JavaScript operators include:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators
  • String Operators
  • Conditional (Ternary) Operator
  • Type Operators
  • Comma Operator
  • Unary Operators

Can you explain logical operators in JavaScript?

Logical operators are used to combine multiple Boolean expressions:

  • && (Logical AND)
  • || (Logical OR)
  • ! (Logical NOT)

How does the ternary operator work in JavaScript?

The ternary operator is a shorthand for the if-else statement. It takes three operands:

condition ? expressionIfTrue : expressionIfFalse;

What does the typeof operator do?

The typeof operator returns the type of a variable as a string. For example, typeof 42 returns “number”.

What is the purpose of the instanceof operator?

The instanceof operator checks if an object is an instance of a specific class or constructor. For example, let date = new Date(); date instanceof Date returns true.

We have a list of JavaScript Operators Reference where you can know more about these operators.



Previous Article
Next Article

Similar Reads

JavaScript Course Logical Operators in JavaScript
logical operator is mostly used to make decisions based on conditions specified for the statements. It can also be used to manipulate a boolean or set termination conditions for loops. There are three types of logical operators in Javascript: !(NOT): Converts operator to boolean and returns flipped value &amp;&amp;:(AND) Evaluates operands and retu
3 min read
JavaScript Course Operators in JavaScript
An operator is capable of manipulating a certain value or operand. Operators are used to performing specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands. In JavaScript, operators are used for comparing values, performing arithmetic operations, etc. There are various operators
7 min read
Alternative to Multiple OR Operators in JavaScript
In this article, we will learn the alternatives to the use of multiple || (OR operator) in conditional statements. Multiple OR operators in conditional statements might cause poor readability of code and are often limited in functionality. Example: The below JavaScript code could be rewritten to perform the same task in other ways: C/C++ Code let u
4 min read
How to use multiple ternary operators in a single statement in JavaScript ?
In this article, we will see how we can use multiple ternary operators in a single statement in JavaScript. Sometimes using conditional statements like if, else, and else if stretches the code unnecessarily and makes the code even more lengthy. One trick to avoid them can be the use of ternary operators. But what if we are to use multiple if-else s
2 min read
Javascript Short Circuiting Operators
In JavaScript short-circuiting, an expression is evaluated from left to right until it is confirmed that the result of the remaining conditions is not going to affect the already evaluated result. If the result is clear even before the complete evaluation of the expression, it short circuits and the result will be returned. Short circuit evaluation
3 min read
What are Comparison Operators in JavaScript ?
Comparison operators in JavaScript are used to compare two values and return a boolean result (true or false). They include operators such as ==, ===, !=, !==, &gt;, &lt;, &gt;=, and &lt;=. Example: Here, the &gt; (greater than) the operator compares the values of a and b. Since a is not greater than b, the expression a &gt; b evaluates to false, w
1 min read
JavaScript String Operators
JavaScript String Operators are used to manipulate and perform operations on strings. There are two operators which are used to modify strings in JavaScript. These operators help us to join one string to another string. Type of JavaScript String Operators There are two type of String Operators in JavaScript, these are: String Concatenate OperatorSt
2 min read
JavaScript Relational operators
JavaScript Relational Operators are used to compare their operands and determine the relationship between them. They return a Boolean value (true or false) based on the comparison result. Types of Relational OperatorsThese are two types of relational operators, these are: JavaScript in OperatorJavaScript instanceof OperatorWe will explore all the a
2 min read
JavaScript Unary Operators
JavaScript Unary Operators work on a single operand and perform various operations, like incrementing/decrementing, evaluating data type, negation of a value, etc. Table of Content Unary Plus (+) OperatorUnary Minus (-) OperatorUnary Increment (++) OperatorUnary Decrement (--) OperatorLogical NOT (!) OperatorBitwise NOT (~) Operatortypeof Operatord
6 min read
JavaScript Assignment Operators
Assignment Operators Assignment operators are used to assign values to variables in JavaScript. Syntax: data=valueExample: // Lets take some variablesx=10y=20x=y // Here, x is equal to 20y=x // Here, y is equal to 10Assignment Operators ListThere are so many assignment operators as shown in the table with the description. OPERATOR NAMESHORTHAND OPE
5 min read
three90RightbarBannerImg