# JavaScript Operators

Operators are symbols or keywords that instruct the JavaScript engine to take certain actions. The addition **(+)** sign, for example, instructs the JavaScript engine to add two variables or values, whereas the equal-to **(==)**, greater-than **(>)**, or less-than () symbols instruct the JavaScript engine to compare two variables or values, and so on.

A value or operand can be manipulated by an operator. Operators are used on operands to accomplish specialized mathematical and logical operations. To put it another way, an operator controls the operands. Operators are used in JavaScript to compare values, perform arithmetic operations, and so on.

JavaScript, like other languages, has operators. An operator is a program that performs an operation on a single or more operands (data values) and returns a result. In 1 + 2, for example, the + sign is an operator, and 1 is the left side operand and 2 is the right side operand. The + operator combines two numeric values and returns the result.

## Arithmetic operator

An arithmetic operator accepts numerical values as operands (either literals or variables) and returns a single numerical value. Addition (+), subtraction (-), multiplication (*), and division (/) are the standard arithmetic operators. When used with floating point numbers, these operators behave similarly to those used in most other programming languages (in particular, note that division by zero produces Infinity).

In addition to the standard arithmetic operations **(+, -, *, /)**, JavaScript provides the following arithmetic operators:

Operator | Operator Name | Description |
---|---|---|

+ | Addition | The addition operator computes the numerical sum of two numeric operands. It also joins two strings or numbers. |

- | Subtraction | The subtraction operator returns the numerical value of the difference between two operands. |

* | Multiplication | The multiplication operator returns the product of two operands, one multiplicand and one multiplier. |

/ | Division | The division operator returns the quotient of its operands, where the divisor is on the right and the dividend is on the left. |

% | Modulus (Remainder) | When a dividend is divided by a divisor, the modulus operator yields the remainder. The residual operator is another name for the modulus operator. It adopts the dividend symbol. |

** | Exponentiation (ES2016) | The result of the exponentiation operator is to raise the first operand to the power of the second operand. Exponentiation is a right-associative operator. |

++ | Increment | The increment operator increases its operand and returns a value. |

-- | Decrement | The decrement operator decrements its operand (subtracts one from it) and returns a value. |

- | Unary | This is a unary operator, which means it only has one operand. It returns the inverse of an operand. |

+ | Unary | This is a method for converting a non-number to a number. Although unary negation (-) can also convert non-numbers, unary plus is the quickest and most preferred method of converting something into a number because it does not perform any additional operations on the number. |

## Assignment operator

The equal **(=) **operator assigns the value of the right-hand operand to the left-hand operand. If **a = b**, the value of b is assigned to a.

A variable is assigned a value using the simple assignment operator. The assigned value is evaluated by the assignment process. It is possible to chain the assignment operator to assign a single value to multiple variables.

Operator | Meaning | Name | Description |
---|---|---|---|

a+=b | a=a+b | Addition | This operator adds the right operand's value to a variable and assigns the result to the variable. The behavior of the addition assignment operator is determined on the types of the two operands. It is possible to add or concatenate. If concatenation is used, the string is used as an operand. |

a-=b | a=a-b | Subtraction | Subtracts the right operand's value from a variable and assigns the result to the variable. |

a*=b | a=a*b | Multiplication | This operator multiplies the value of the right operand by a variable and assigns the result to the variable. |

a/=b | a=a/b | Division | This operator divides a variable by the right operand's value and assigns the result to the variable. |

a%=b | a=a%b | Remainder | This operator divides a variable by the right operand's value and assigns the remainder to the variable. |

a**=b | a=a**b | Exponentiation | This operator elevates a variable's value to the power of the argument on the right. |

a<<=b | a=a<<b | Left Shift | This operator shifts the number of bits supplied to the left and assigns the result to the variable. |

a>>=b | a=a>>b | Right Shift | This operator shifts the number of bits supplied to the right and assigns the result to the variable. |

a&=b | a=a&b | Bitwise AND | This operator takes the binary representations of both operands and performs a bitwise AND operation on them before assigning the result to a variable. |

a|=b | a=a|b | Bitwise OR | This operator takes both operands' binary representations, performs a bitwise OR operation on them, and assigns the result to a variable. |

a^=b | a=a^b | Bitwise XOR | This operator takes both operands' binary representations, performs a bitwise XOR operation on them, and assigns the result to the variable. |

## String operator

In addition to the comparison operators that may be used to string values, the concatenation operator **(+)** joins two string values, yielding another string that is the union of the two operand strings.

Operator | Description | Example | Result |
---|---|---|---|

+ | Concatenation | str1 + str2 | Concatenation of str1 and str2 |

+= | Concatenation assignment | str1 += str2 | Appends the str2 to the str1 |

## Incrementing and Decrementing operator

The increment **(++)** operator increases its operand (adds one to it) and returns the value before or after the increment, depending on where the operator is inserted.

Depending on where the operator is used, the decrement **(--)** operator decrements (subtracts one from) its argument and returns the value before or after the decrement.

Operator | Name | Description |
---|---|---|

++x | Pre-increment | Increments x by one, then returns x |

x++ | Post-increment | Returns x, then increments x by one |

--x | Pre-decrement | Decrements x by one, then returns x |

x-- | Post-decrement | Returns x, then decrements x by one |

## Comparison operator

A comparison operator compares its operands and returns a logical value based on the result of the comparison. Numerical, string, logical, or object values can be used as operands. Strings are compared using Unicode values and conventional lexicographical ordering. If the two operands are not of the same type, JavaScript will usually try to convert them to an acceptable type for the comparison.

This behavior usually leads to a numerical comparison of the operands. The **===** and **!==** operators, which conduct rigorous equality and inequality comparisons, are the only exceptions to type conversion within comparisons. Before testing equality, these operators do not attempt to transform the operands to compatible types.

Operator | Name | Example | Result |
---|---|---|---|

== | Equal | x == y | True if x is equal to y |

=== | Identical | x === y | True if x is equal to y, and they are of the same type |

!= | Not equal | x != y | True if x is not equal to y |

!== | Not identical | x !== y | True if x is not equal to y, or they are not of the same type |

< | Less than | x < y | True if x is less than y |

> | Greater than | x > y | True if x is greater than y |

>= | Greater than or equal to | x >= y | True if x is greater than or equal to y |

<= | Less than or equal to | x <= y | True if x is less than or equal to y |

## Logical operator

When logical operators are employed with Boolean (logical) values, they yield a Boolean value. However, because the **&&** and **||** operators return the value of one of the given operands, they may produce a non-Boolean value if used with non-Boolean values. The logical operators are listed in the table below.

Operator | Name | Example | Result |
---|---|---|---|

&& | And | x && y | True if both x and y are true |

|| | Or | x || y | True if either x or y is true |

! | Not | !x | True if x is not true |

## FAQs

JavaScript operators are symbols or keywords that perform operations on operands to produce a result. They are used for performing mathematical calculations, comparing values, assigning values, and more. Operators are crucial in programming as they enable developers to manipulate data, make decisions, and perform various actions based on the desired logic. JavaScript includes **arithmetic**, **comparison**, **logical**, **assignment**, and other types of operators that help build complex expressions and algorithms.

JavaScript comparison operators are used to compare values and return a Boolean result **(true or false)** based on the comparison. The commonly used comparison operators are **==**, **===**, **!=**, **!==**, **>**, **<**, **>=**, and **<=**. The double equals (==) compares for equality, allowing type coercion if needed, while the triple equals (===) compares for both value and type equality. The not equals operators (!= and !==) perform similar comparisons but return the opposite result. The greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=) operators are used for numerical comparisons.

JavaScript logical operators are used to combine or manipulate Boolean values. The three logical operators in JavaScript are **&&** (logical AND), **||** (logical OR), and **!** (logical NOT). The logical AND (&&) operator returns true if both operands are true; otherwise, it returns false. The logical OR (||) operator returns true if at least one of the operands is true; otherwise, it returns false. The logical NOT (!) operator negates the Boolean value of the operand, returning its opposite value. Logical operators are often used in conditional statements, control flow, and Boolean evaluations to make decisions based on multiple conditions or to manipulate Boolean values.