English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript Basic Tutorial

JavaScript Object

JavaScript Function

JS HTML DOM

JS Browser BOM

AJAX Basic Tutorial

JavaScript Reference Manual

JavaScript Operators

Operators perform mathematical and logical operations on the given operands.

JavaScript has the following types of operators:

  • Arithmetic operator

  • Assignment operator

  • Comparison operator

  • Bitwise operator

  • Logical operator

  • String operator

  • Conditional (ternary) operator

  • unary operator

  • relation operator

JavaScript arithmetic operators

Arithmetic operators are used to perform arithmetic operations on numbers.

Arithmetic operators take numbers as their operands and return a single number.

OperatorDescriptionExampleDefinitionTest it
+additiona + bsum of a and bTest it
-subtractiona-bdifference between a and bTest it
*multiplicationa * bproduct of a and bTest it
**exponentiationa ** ba to the power of bTest it
/divisiona / ba divided by bTest it
%modulus (remainder)a % ba / remainder of bTest it
++incrementa ++a increment1Test it
--decrement
a--a decrement1Test it

JavaScript assignment operators

Assignment operators assign values to JavaScript variables.

Simple assignment operator equals (=), it assigns the value of its right operand to its left operand.

OperatorDescriptionExampleequal toTest it
=assignmenta = ba = bTest it
+=assignment after additiona + = ba = a + bTest it
-=assignment after subtractiona-= ba = a-bTest it
*=assignment after multiplicationa * = ba = a * bTest it
**=assignment after exponentiationa ** = ba = a ** bTest it
/=assignment after divisiona / = ba = a / bTest it
assignment after modulusa %= ba = a % bTest it

JavaScript comparison operators

Comparison operators are used to compare two values and return a boolean value.

OperatorDescriptionExampleDefinitionTest it
==equal toa == btrue if a is equal to bTest it
===samea === btrue if a is equal to b and belong to the same typeTest it
!=not equala != btrue if a is not equal to bTest it
!==not equal including typea !== btrue if a is not equal to b and not the same data typeTest it
>greater thana > btrue if a is greater than bTest it
<less thana < btrue if a is less than bTest it
>=greater than or equal toa >= btrue if a is greater than or equal to bTest it
<=less than or equal toa <= btrue if a is less than or equal to bTest it

The difference between (a == b) and (a === b) is:

  • == if a equals b, then return true

  • === if a equals b and they belong tosame typethen return true

Comparison operators can be used in conditional statements to compare values and enter different processes based on the result.

Later in this tutorial, you will learn how to use conditional statements.

JavaScript bitwise operators

Bitwise operators treat their operands as a set of32bits (zero and one), rather than decimal, hexadecimal, or octal numbers.

Any number operand in this operation will be converted to32The result is converted back to JavaScript number.

OperatorDescriptionExampleequal toresultdecimal
&and5&10101and 00010001 1bit
|or5 | 10101 | 00010101 5
~not
~5 ~01011010 10
^exclusive or5 ^ 10101 ^ 00010100 4
<<left shift5 << 10101 << 11010 10
>>signed right shift5 >> 10101 >> 10010  2
>>unsigned right shift5 >> 10101 >> 10010  2

The above example uses4unsigned bit example. But JavaScript uses32signed bit number.

Therefore, in JavaScript, ~5will not return10. It will return-6.

~00000000000000000000000000000101will return11111111111111111111111111111010

JavaScript logical operators

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

Logical operators are usually used to combine conditional statements, and they return boolean values.

OperatorDescriptionExampleDefinitionTest it
&&Logical ANDa && bIf a and b are both true, it is trueTest it
||Logical ORa || bIf a or b is true, it is trueTest it
!Logical NOT!aIf a is not true, it is trueTest it

JavaScript string operator

The+The operator can also be used for concatenation (concatenation) of strings.

var str1 = "Hello";
var str2 = "World";
var str3 = str1 + "" + str2;
Test See‹/›

Addition assignment operator+= can also be used for concatenation (concatenation) of strings.

var str1 = "Hey! ";
str1 += "How r u doing?";
Test See‹/›

When used on strings, the+The operator is called the concatenation operator.

JavaScript conditional (ternary) operator

The conditional operator is the only JavaScript operator that uses three operands.

The syntax is that it can obtain one of two values based on the condition.

condition ? val1 : val2

Ifcondition The result of the calculation istrue, then it will returnval1, otherwise it will returnval2.

You can use the conditional operator at any position where you can use standard operators.

var status = (age >= 18) ? "adult" : "child";
Test See‹/›

If the age is18If the age is 18 or above, the statement assigns the value 'adult' to the variable status. Otherwise, it assigns the value 'child' to status.

JavaScript unary operators

A unary operation is an operation that has only one operand.

JavaScript includes three unary operators:

DescriptionDescription
deleteDeletes an object, a property of an object, or an element at a specified index in an array
typeofReturns the type of the variable
voidSpecify an expression that requires a value without returning a value

Delete operator

The delete operator can remove an object, a property of an object, or an element at a specified index in an array.

If the operation can be performed, the delete operator returns true; otherwise, it returns false. If the operation cannot be performed, it returns false.

If the delete operator succeeds, it sets the property or element to undefined:

var user = {firstName:"Vishal", age:"22, color:"blue"}
delete user.firstName;  // returns true
Test See‹/›

When the delete operator deletes an array element, the element will be removed from the array.

var fruits = ["Apple", "Mango", "Banana"];
delete fruits[0];   // delete "Apple"
Test See‹/›

Note:When deleting array elements, the array length is not affected.

typeof operator

The typeof operator returns the type of a variable or expression.

typeof ""  // Returns "string"
typeof "Vishal"// Returns "string"
typeof "42"// Returns "string" (Number within quotes is String)
typeof 42  // Returns "number"
typeof true// Returns "boolean"
typeof false   // Returns "boolean"
typeof undefined   // Returns "undefined"
typeof null// Returns "object"
typeof {name:"Vishal", age:22} // Returns "object"
typeof [2, 4, 6, 8]// Returns "object" (not "array", see note below)
typeof function myFunc(){} // Returns "function"
Test See‹/›

Note:The typeof operator returns "object" for arrays because arrays are objects in JavaScript.

This is the list of values returned by the typeof operator:

Type
The string returned by typeof
Number"number"
String"string"
Boolean"boolean"
Object"object"
Function"function"
Undefined"undefined"
Null"object"

Void operator

The void operator specifies an expression that requires a value but does not return a value.

The following code creates a hyperlink that does nothing when the user clicks on it.

<a href="javascript:void(0)">Click here to do nothing</a>/a>
Test See‹/›

JavaScript relational operators

Relational operators compare their operands and return a boolean value based on whether the comparison is true.

JavaScript has two relational operators:

DescriptionDescription
inIf the specified property exists in the specified object, it returns true
instanceofreturns true if the object is an instance of an object type

in operator

If the specified property exists in the specified object, the in operator returns true.

var user = {firstName:"Vishal", age:"22, color:"blue"}
"firstName" in user;  // returns true
"age" in user;// returns true
"Food" in user;   // returns false
Test See‹/›

When using the in operator with an Array, you must specify the index number, not the value at that index.

var fruits = ["Apple", "Mango", "Banana"];
var item1 = 0 in fruits;  // returns true
var item2 = 1 in fruits;  // returns true
var item3 = 2 in fruits;  // returns true
var item4 = 3 in fruits;  // returns false
Test See‹/›

instanceof operator

If the specified object is of the specified type, the instanceof operator will return true.

Use the instanceof operator when you need to check the type of an object at runtime.

var myObj = new Date();
if (myObj instanceof Date) {
   // statements to execute
}
Test See‹/›