JavaScript Tutorial || JavaScript Operators

JavaScript Operators


Operators are symbols or keywords that tell the JavaScript engine to perform some sort of actions.For example, 5 + 2 is equal to 7. Here 5 and 2 are called operands and ‘+’ is called the operator. JavaScript supports the following types of operators.


  1. Arithmetic Operators
  2. Comparison Operators
  3. Logical (or Relational) Operators
  4. Assignment Operators
  5. Conditional (or ternary) Operators
Arithmetic Operators

The arithmetic operators are used to perform common arithmetical operations, such as addition, subtraction, multiplication etc.

OperatorDescriptionExampleResult
+Addition$x + $ySum of $x and $y
-Subtraction$x - $yDifference of $x and $y.
*Multiplication$x * $yProduct of $x and $y.
/Division$x / $yQuotient of $x and $y
%Modulus$x % $yRemainder of $x divided by $y

example

var x = 6;var y = 4;(x + y)= 10

Assignment Operators

The assignment operators are used to assign values to variables.


OperatorDescriptionExampleIs The Same As
=Assignx = yx = y
+=Add and assignx += $x = x + y
-=Subtract and assignx -= yx = x - y
*=Multiply and assignx *= yx = x * y
/=Divide and assign quotientx /= yx = x / y
%=Divide and assign modulusx %= yx = x % y

example
var x; x = 10; x= 10x = 20;x += 30; x=50
String Operators

There are two operators which can also used be for strings.


OperatorDescriptionExampleResult
+Concatenationstr1 + str2Concatenation of str1 and str2
+=Concatenation assignmentstr1 += str2Appends the str2 to the str1

example
var str1 = "Hello";var str2 = " World!";str1 + str2 = Hello World!

Incrementing and Decrementing Operators

The increment/decrement operators are used to increment/decrement a variable's value.

OperatorNameEffect
++xPre-incrementIncrements x by one, then returns x
x++Post-incrementReturns x, then increments x by one
--xPre-decrementDecrements x by one, then returns x
x--Post-decrementReturns x, then decrements x by one

example
var x; x = 10;alert(++x); // Outputs: 11alert(x); // Outputs: 11

Logical Operators

The logical operators are typically used to combine conditional statements.

OperatorNameExampleResult
&&Andx && yTrue if both x and y are true
||Orx || yTrue if either x or y is true
!Not!xTrue if x is not true

example
var age = 20;
if((age == 20) || ((age != 0))){ alert("done");} else{ alert("false");}

Comparison Operators

The comparison operators are used to compare two values in a Boolean fashion.

OperatorNameExampleResult
==Equalx == yTrue if x is equal to y
===Identicalx===yTrue if x is equal to y, and they are of the same type
!=Not equalx != yTrue if x is not equal to y
!==Not identicalx!==yTrue if x is not equal to y, or they are not of the same type
<Less thanx < yTrue if x is less than y
>Greater thanx > yTrue if x is greater than y
>=Greater than or equal tox >= yTrue if x is greater than or equal to y
<=Less than or equal tox <= yTrue if x is less than or equal to y

example
var x = 25;var y = 35;var z = "25"; alert(x == z); // Outputs: truealert(x === z); // Outputs: falsealert(x != y); // Outputs: true