JavaScript Operators

JavaScript Operators are special symbols used to perform operations on operands (values and variables). JavaScript operators include operators as in other languages. For example, 1 * 2, where * sign is an operator and 1 is left operand and 2 is a right operand. + operator adds two numeric values and produces a result which is 3 in this case.

Here * is a JavaScript operator that performs multiplication, and 2 and 3 are operands.

Operators and Operands

The numbers (in an arithmetic operation) are called operands.

The operation (to be performed between the two operands) is defined by an operator.

The multiplication operator (*) multiply numbers:

JavaScript Operators – types

  • Assignment Operators
  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators
  • String Operators
  • Other Operators

JavaScript Assignment Operators

Assignment operators are used for assigning values to variables. For example,

Here, the = the operator is used to assign value 5 to variable x.

Here’s a list of commonly used assignment operators:

OperatorNameExample
=Assignment operatora = 7; // 7
+=Addition assignmenta += 5; // a = a + 5
-=Subtraction Assignmenta -= 2; // a = a - 2
*=Multiplication Assignmenta *= 3; // a = a * 3
/=Division Assignmenta /= 2; // a = a / 2
%=Remainder Assignmenta %= 2; // a = a % 2
**=Exponentiation Assignmenta **= 2; // a = a**2

Note: The commonly used assignment operator is =. You will understand other assignment operators such as +=-=*= etc. once we learn arithmetic operators.

<!DOCTYPE html>
<html>
<body>

<p id="tacode"></p>

<script>
var y = 15;
document.getElementById("tacode").innerHTML = y;
</script>

</body>
</html>

Check its output:

JavaScript Operators

JavaScript Arithmetic Operators

Arithmetic JavaScript operators are used to perform arithmetic calculations. For example:

Here, the + operator is used to add two operands.

OperatorNameExample
+Additionx + y
-Subtractionx - y
*Multiplicationx * y
/Divisionx / y
%Remainderx % y
++Increment (increments by 1)++x or x++
--Decrement (decrements by 1)--x or x--
**Exponentiation (Power)x ** y
<!DOCTYPE html>
<html>
<body>

<p id="tacode"></p>

<script>
var y = 150 + 50;
document.getElementById("tacode").innerHTML = y;
</script>

</body>
</html>

Output

JS Operators

JavaScript Logical Operators

Logical operators perform logical operations and return a boolean value, either true or false.

OperatorDescriptionExample
&&Logical AND: true if both the operands are true, else returns falsex && y
||Logical OR: true if either of the operands is true; returns false if both are falsex || y
!Logical NOT: true if the operand is false and vice-versa.!x

JavaScript Comparison Operators

JavaScript language includes operators that compare two operands and return the Boolean values true or false.

OperatorsDescription
==Compares the equality of two operands without considering type.
===Compares equality of two operands with type.
!=Compares inequality of two operands.
>Checks whether the left-side value is greater than the right-side value. If yes then returns true otherwise false.
<Checks whether the left operand is less than the right operand. If yes then returns true otherwise false.
>=Checks whether the left operand is greater than or equal to the right operand. If yes then returns true otherwise false.
<=Checks whether the left operand is less than or equal to the right operand. If yes then returns true otherwise false.

JavaScript Ternary Operator

JavaScript includes a special operator called ternary operator : ? that assigns a value to a variable based on some condition. This is like a short form of the if-else condition.

Syntax:

The ternary operator starts with conditional expression followed by ? operator. The second part ( after ? and before : operator) will be executed if the condition turns out to be true. If the condition becomes false then the third part (after 🙂 will be executed.

JS Bitwise Operators

Bitwise operators perform operations on binary representations of numbers.

OperatorDescription
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise NOT
<<Left shift
>>Sign-propagating right shift
>>>Zero-fill right shift

Bitwise operators are rarely used in everyday programming.