Open In App

JavaScript Operators

Last Updated : 30 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

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

JavaScript Chaining Operator (?.)

The Optional Chaining Operator (?.) allows you to access deeply nested properties of an object without having to check for the existence of each level in the chain. If any part of the chain is null or undefined, it will return undefined instead of throwing an error.

Name

Description

Syntax

Example

Optional Chaining (?.)

Safely accesses properties, methods, or

array elements of deeply nested objects.

If any part of the chain is null or undefined,

it returns undefined instead

let result = object?.property

let user = {

name: “John”, address: { city: “New York” }};

console.log(user?.address?.city);

console.log(user?.address?.zipcode);

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.



Next Article

Similar Reads

three90RightbarBannerImg
  翻译: