English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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
Arithmetic operators are used to perform arithmetic operations on numbers.
Arithmetic operators take numbers as their operands and return a single number.
Operator | Description | Example | Definition | Test it |
---|---|---|---|---|
+ | addition | a + b | sum of a and b | Test it |
- | subtraction | a-b | difference between a and b | Test it |
* | multiplication | a * b | product of a and b | Test it |
** | exponentiation | a ** b | a to the power of b | Test it |
/ | division | a / b | a divided by b | Test it |
% | modulus (remainder) | a % b | a / remainder of b | Test it |
++ | increment | a ++ | a increment1 | Test it |
-- | decrement | a-- | a decrement1 | Test it |
Assignment operators assign values to JavaScript variables.
Simple assignment operator equals (=), it assigns the value of its right operand to its left operand.
Operator | Description | Example | equal to | Test it |
---|---|---|---|---|
= | assignment | a = b | a = b | Test it |
+= | assignment after addition | a + = b | a = a + b | Test it |
-= | assignment after subtraction | a-= b | a = a-b | Test it |
*= | assignment after multiplication | a * = b | a = a * b | Test it |
**= | assignment after exponentiation | a ** = b | a = a ** b | Test it |
/= | assignment after division | a / = b | a = a / b | Test it |
assignment after modulus | a %= b | a = a % b | Test it |
Comparison operators are used to compare two values and return a boolean value.
Operator | Description | Example | Definition | Test it |
---|---|---|---|---|
== | equal to | a == b | true if a is equal to b | Test it |
=== | same | a === b | true if a is equal to b and belong to the same type | Test it |
!= | not equal | a != b | true if a is not equal to b | Test it |
!== | not equal including type | a !== b | true if a is not equal to b and not the same data type | Test it |
> | greater than | a > b | true if a is greater than b | Test it |
< | less than | a < b | true if a is less than b | Test it |
>= | greater than or equal to | a >= b | true if a is greater than or equal to b | Test it |
<= | less than or equal to | a <= b | true if a is less than or equal to b | Test 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.
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.
Operator | Description | Example | equal to | result | decimal |
---|---|---|---|---|---|
& | and | 5&1 | 0101and 0001 | 0001 | 1bit |
| | or | 5 | 1 | 0101 | 0001 | 0101 | 5 |
~ | not | ~5 | ~0101 | 1010 | 10 |
^ | exclusive or | 5 ^ 1 | 0101 ^ 0001 | 0100 | 4 |
<< | left shift | 5 << 1 | 0101 << 1 | 1010 | 10 |
>> | signed right shift | 5 >> 1 | 0101 >> 1 | 0010 | 2 |
>> | unsigned right shift | 5 >> 1 | 0101 >> 1 | 0010 | 2 |
The above example uses4unsigned bit example. But JavaScript uses32signed bit number.
Therefore, in JavaScript, ~5will not return10. It will return-6.
~00000000000000000000000000000101will return11111111111111111111111111111010
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.
Operator | Description | Example | Definition | Test it |
---|---|---|---|---|
&& | Logical AND | a && b | If a and b are both true, it is true | Test it |
|| | Logical OR | a || b | If a or b is true, it is true | Test it |
! | Logical NOT | !a | If a is not true, it is true | Test it |
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.
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.
A unary operation is an operation that has only one operand.
JavaScript includes three unary operators:
Description | Description |
---|---|
delete | Deletes an object, a property of an object, or an element at a specified index in an array |
typeof | Returns the type of the variable |
void | Specify an expression that requires a value without returning a value |
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 trueTest 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.
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" |
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‹/›
Relational operators compare their operands and return a boolean value based on whether the comparison is true.
JavaScript has two relational operators:
Description | Description |
---|---|
in | If the specified property exists in the specified object, it returns true |
instanceof | returns true if the object is an instance of an object type |
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 falseTest 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 falseTest See‹/›
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‹/›