Friday, December 27, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Operators Reference

JavaScript Operators Reference

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 to compare values, perform arithmetic operations, etc.

Example:  In this example, we will use an operator to perform an operation.

Javascript




let a = 17;
let b = "neveropen";
let c = "";
let d = null;
  
console.log("Type of a = " + (typeof a));
console.log("Type of b = " + (typeof b));
console.log("Type of c = " + (typeof c));
console.log("Type of d = " + (typeof d));
console.log("Type of e = " + (typeof e));


Output: As you can see, we have used the typeof operator to check the variable type.

Type of a = number
Type of b = string
Type of c = string
Type of d = object
Type of e = undefined

The Complete List of JavaScript Operators is listed below:

JavaScript Arithmetic Operators: These are the operators that work on numerical values and then return a number. These are basically used to perform mathematical operations.

OPERATOR NAME

OPERATION

Addition(+) Add two numbers or concatenate the string.
Subtraction(-) Difference between the two operators.
Multiplication(*) Multiply two numbers.
Division(/) Find the quotient of two operands.
Modulus(%) Find the remainder of two operands.
Exponentiation(**) Raise the Left operator to the power of the right operator.
Increment(++) Increase the operand by one.
Decrement(–) Decrease the operand by one.
Unary Plus(+) Converts non-numbers to numbers.
Unary Negation (-) Converts operand to negative.

JavaScript Comparison Operators: These are the operators that are used to perform equality or difference comparisons between the values. It checks whether an element is greater, smaller equal, or unequal to the other element.

OPERATOR NAME

OPERATION

Equality(==) Compares the equality of two operators.
Inequality(!=) Compares inequality of two operators.
Strict Equality(===) Compares both value and type of the operand.
Strict Inequality(!==) Compares inequality with type.
Greater than(>) Checks if the left operator is greater than the right operator.
Greater than or equal(>=) Checks if the left operator is greater than or equal to the right operator.
Less than(<) Checks if the left operator is smaller than the right operator.
Less than or equal(<=) Checks if the left operator is smaller than or equal to the right operator.

JavaScript Logical Operators: These are the operators which allow us to compare variables or values. The 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.

OPERATOR NAME OPERATION
NOT(!) Converts operator to boolean and returns flipped value.
AND(&&) Evaluates operands and return true only if all are true.
OR(||) Returns true even if one of the multiple operands is true.

JavaScript Bitwise Operators: These operators 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. 
 

OPERATOR NAME OPERATION
Bitwise AND(&) Returns true if both operands are true.
Bitwise OR(|) Returns true even if one operand is true.
Bitwise XOR(^) Returns true if both operands are different.
Bitwise NOT(~) Flips the value of the operand.
Bitwise Left Shift(<<) Shifts the bit toward left.
Bitwise Right Shift(>>) Shifts the bit towards the right.
Zero Fill Right Shift(>>>) Shifts the bit towards the right but adds 0 from the left.

JavaScript Assignment Operators: These operators assign the value of the right-hand operand to its left-hand operand. That is if a = b assigns the value of b to a.

OPERATOR NAME OPERATION
Addition Assignment(+=) Add the value of the right operand to left and assign it to the left operand.
Subtraction Assignment(-=) Subtracts the value of the right operand to left and assigns it to the left operand.
Multiplication Assignment(*=) Multiplies the value of the right operand to left and assign it to the left operand.
Division Assignment(/=) Divides the value of the right operand by the left and assigns it to the left operand.
Remainder Assignment(%=) Finds the remainder after the division of the right operand with the left and assign it to the left operand.
Exponentiation Assignment(**=) Raises left operand to power of right operand and assign it to the left operand.
Left Shift Assignment(<<=) Moves the bits specified on the right operand to left and then assigns it to the left operand.
Right Shift Assignment(>>=) Moves the bits specified on the right operand to right and then assigns it to the left operand.
Bitwise AND Assignment(&=) Does a bitwise AND operation of both operands and assign it to the left operand.
Bitwise OR Assignment(|=) Does a bitwise OR operation of both operands and assign it to the left operand.
Bitwise XOR Assignment(^=) Does a bitwise XOR operation of both operands and assigns it to the left operand.

Few Important JavaScript Operators:

OPERATOR NAME OPERATION
Ternary(?:) Used as a simplified version of if-else to reduce code lines.
typeof  A keyword to return the data type of the operand.
conditional Used to perform operations according to conditions.
instanceof Used to check the type of operator at runtime.
Delete Used to delete JavaScript object properties.
in  Keyword used to check if a property exists or not.
Spread(…) A new feature is used to expand iterables in form of an array.
comma(,) Used to separate operands.
Grouping Used to override normal operator precedence.
Unary(+_)  Converts the type of variable to the number.
Short circuiting Used for evaluating an expression.
Nullish Coalescing(??)  Used to return a value if the value is undefined or null.
Unsigned right bit shift>>> Used as unsigned right bit shift operator.
Pipeline(|>) Used to pipe the value of an expression to function.
Optional chaining(?.) Error-proof way to access nested object properties.
Arrow  Shortcut way of writing a function.

To learn about the precedence of these operators check this article Operator precedence in JavaScript

RELATED ARTICLES

Most Popular

Recent Comments