# JavaScript Operators

JavaScript operators operate the operands, these are symbols that are used to manipulate a certain value or operand. Operators are used to performing specific mathematical and logical computations on operands.

In JavaScript, operators are used to compare values, perform arithmetic operations, etc.

JavaScript Operators: There are various operators supported by JavaScript.

## JavaScript Arithmetic Operators

 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 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 ‘/’ 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

These operators are used to determine the logic between variables or values.

 Name Description Syntax Example 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. 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<>) 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 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’]

## JavaScriptRelational 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

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

 Name Description Syntax Example It concatenates two string values together, returning another string that is the union of the two operand strings. str1 + str2

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

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next