JavaScript Operator Reference Manual
- Previous Page JS Objects
- Next Page JS Operator Precedence
JavaScript operators are used for assignment, comparison of values, execution of arithmetic operations, etc.
See also:
Tutorial:JavaScript Operators
Tutorial:JavaScript Operator Precedence
JavaScript Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations between variables and/or values.
Given y = 5The following table explains the arithmetic operators:
Operator | Description | Example | The result in y | The result in x | Try it |
---|---|---|---|---|---|
+ | Addition | x = y + 2 | y = 5 | x = 7 | Try it |
- | Subtraction | x = y - 2 | y = 5 | x = 3 | Try it |
* | Multiplication | x = y * 2 | y = 5 | x = 10 | Try it |
/ | Division | x = y / 2 | y = 5 | x = 2.5 | Try it |
% | Modulus (remainder of division) | x = y % 2 | y = 5 | x = 1 | Try it |
++ | Increment | x = ++y | y = 6 | x = 6 | Try it |
x = y++ | y = 6 | x = 5 | Try it | ||
-- | Decrement | x = --y | y = 4 | x = 4 | Try it |
x = y-- | y = 4 | x = 5 | Try it |
For knowledge about arithmetic operators, please read our JavaScript Arithmetic Tutorial.
JavaScript Assignment Operators
Assignment operators are used to assign values to JavaScript variables.
Given x = 10 and y = 5The following table explains the assignment operators:
Operator | Example | Is equivalent to | The result in x | Try it |
---|---|---|---|---|
= | x = y | x = y | x = 5 | Try it |
+= | x += y | x = x + y | x = 15 | Try it |
-= | x -= y | x = x - y | x = 5 | Try it |
*= | x *= y | x = x * y | x = 50 | Try it |
/= | x /= y | x = x / y | x = 2 | Try it |
%= | x %= y | x = x % y | x = 0 | Try it |
For knowledge about assignment operators, please read our JavaScript Assignment Tutorial.
JavaScript String Operators
+ operator and += operator can also be used to concatenate (add) strings.
Given text1 = "Good "、text2 = "Morning"as well as text3 = ""The following table explains this operator:
Operator | Example | text1 | text2 | text3 | Try it |
---|---|---|---|---|---|
+ | text3 = text1 + text2 | "Good " | "Morning" | "Good Morning" | Try it |
+= | text1 += text2 | "Good Morning" | "Morning" | "" | Try it |
Comparison operators
Use comparison operators in logical statements to determine the equality or difference between variables or values.
Given x = 5The following table explains the comparison operators:
Operator | Description | Comparison | Return | Try it |
---|---|---|---|---|
== | Equal to | x == 8 | false | Try it |
x == 5 | true | Try it | ||
=== | Equal values and equal types | x === "5" | false | Try it |
x === 5 | true | Try it | ||
!== | Not equal | x != 8 | true | Try it |
!== | Unequal values or unequal types | x !== "5" | true | Try it |
x !== 5 | false | Try it | ||
> | Greater than | x > 8 | false | Try it |
< | Less than | x < 8 | true | Try it |
>= | Greater than or equal to | x >= 8 | false | Try it |
<= | Less than or equal to | x <= 8 | true | Try it |
For knowledge about comparison operators, please read our JavaScript Comparison Tutorial.
Conditional (ternary) operator
The conditional operator assigns a value to a variable based on the condition.
Syntax
variablename = (condition) ? value1:value2
Example
voteable = (age < 18) ? 'Too young':'Old enough';
Example explanation:
If the value of the variable 'age' is less than 18, the value of the variable 'voteable' will be 'Too young', otherwise, the value of 'voteable' will be 'Old enough'.
Logical operators
Logical operators are used to determine the logic between variables or values.
Given x = 6 and y = 3The following table explains the logical operators:
Operator | Description | Example | Try it |
---|---|---|---|
&& | and | (x < 10 && y > 1) is true | Try it |
|| | or | (x === 5 || y === 5) is false | Try it |
! | not | !(x === y) is true | Try it |
JavaScript bitwise operators
Bitwise operators can handle 32-bit numbers. Any number operand in this operation will be converted to a 32-bit number. The result will be converted back to a JavaScript number.
Operator | Description | Example | Is equivalent to | Result | Decimal |
---|---|---|---|---|---|
& | AND | x = 5 & 1 | 0101 & 0001 | 0001 | 1 |
| | OR | x = 5 | 1 | 0101 | 0001 | 0101 | 5 |
~ | NOT | x = ~5 | ~0101 | 1010 | 10 |
^ | XOR | x = 5 ^ 1 | 0101 ^ | 0100 | 4 |
<< | Left shift | x = 5 << 1 | 0101 << 1 | 1010 | 10 |
>> | Right shift | x = 5 >> 1 | 0101 >> 1 | 0010 | 2 |
The above example uses a 4-bit unsigned example. However, JavaScript uses 32-bit signed numbers.
Therefore, in JavaScript, ~5 will not return 10, but -6.
~00000000000000000000000000000101 will return 11111111111111111111111111111010
typeof operator
typeof The operator returns the type of a variable, object, function, or expression:
Example
typeof "Bill" // Returns string typeof 3.14 // Returns number typeof NaN // Returns number typeof false // Returns boolean typeof [1, 2, 3, 4] // Returns object typeof {name:'Bill', age:19} // Returns object typeof new Date() // Returns object typeof function () {} // Returns function typeof myCar // Returns undefined (if myCar is not declared) typeof null // Returns object
Please note:
- The data type of NaN is number
- The data type of an array is object
- The data type of a date is object
- The data type of null is object
- The data type of an undefined variable is undefined
You cannot use typeof to define if a JavaScript object is an array (or a date).
You cannot use typeof to determine if a JavaScript object is an array (or a date).
delete operator
delete The operator deletes properties from an object:
Example
var person = {firstName:"Bill", lastName:"Gates", age:19, eyeColor:"blue"}; delete person.age; // Equivalent to delete person["age"];
The delete operator removes both the value and the property itself.
The property cannot be used until it is added back after deletion.
The delete operator is intended to be used for object properties. It has no effect on variables or functions.
Note:Do not use the delete operator on predefined JavaScript object properties. This may cause your application to crash.
in operator
if the specified property exists in the specified object, then in The operator will return true, otherwise return false:
Example
// Arrays var cars = ["Saab", "Volvo", "BMW"]; "Saab" in cars // Returns false (specifies an index number rather than a value) 0 in cars // Returns true 1 in cars // Returns true 4 in cars // Returns false (does not exist) "length" in cars // Returns true (length is an array property) // Objects var person = {firstName:"Bill", lastName:"Gates", age:19}; "firstName" in person // Returns true "age" in person // Returns true // Predefined objects "PI" in Math // Returns true "NaN" in Number // Returns true "length" in String // Returns true
instanceof operator
if the specified object is an instance of the specified object, then instanceof The operator returns true:
Example
var cars = ["Saab", "Volvo", "BMW"]; cars instanceof Array; // Returns true cars instanceof Object; // Returns true cars instanceof String; // Returns false cars instanceof Number; // Returns false
void operator
void operator evaluates an expression and returns undefined. This operator is typically used to obtain the undefined original value using "void(0)" (very useful when evaluating an expression without using the returned value).
Example
<a href="javascript:void(0);"> Useless Link </a> <a href="javascript:void(document.body.style.backgroundColor='red');"> Click me to change the background color of body to red </a>
- Previous Page JS Objects
- Next Page JS Operator Precedence