Operators
An operator performs some operation on single or multiple operands and produces a result.
/* Most operators are binary or unary
<left operand> operator <right operand>
<left operand> operator
*/
Arithmetic
Operator | Description |
+ |
Addition. Can be used with strings.
a = 23+b;
var nick="Nick"+10; // result string "Nick10"
var name=23 +"Name"+10; // result string "23Name10"
|
- | Subtraction
var a = b-10;
|
* | Multiplication
var a = b*10;
|
** | Exponentiation
var a = 2**3; // now a equal to 8
|
/ | Division
a = b/10;
|
% | Remainder
a = 24%10; // result 4
|
++ | Increment. You can use postfix increment or prefix increment.
var ind=10;
ind++; // now ind equal to 11
var x = ind++; // now x equal to 11, ind equal to 12
var y = ++ind; // now y and ind equal to 13
|
-- | Decrement. You can use postfix decrement or prefix increment.
var ind=10;
ind--; // now ind equal to 9
|
Relational
Operator | Description |
== |
Is equal to. Before comparing, it tries to convert the type of the operands, so 2=='2' is true. See more examples of equality in Javascript.
|
=== | Is identical, i.e. both operands refer to the same object or operands have same type and value for primitive types.
if(a===b){
...
}
See more examples of equality in Javascript
|
!= | Not equal to
if(a!=34){
...
}
|
!== | Not Identical
if(a!==b){
...
}
|
< | Less than
var a = 23 < 45; // now a equal to true
|
<= | Less than or equal to
var a = 46 <= 45; // now a equal to false
|
> | Greater than
var a = 23 > 45;
|
>= | Greater than or equal to
var a = 46 >= 45;
|
Bitwise
Operator | Description |
& |
Bitwise and
var a = 23&46; // now a equal to 6
|
| |
Bitwise or
var a = 23|46; // now a equal to 63
|
^ |
Bitwise xor
var a = 23^46; // now a equal to 57
|
~ |
Bitwise not
var a = ~23; // now a equal to -24
|
<< |
Bitwise left shift
var a = 23<<4;// now a equal to 368
|
>> |
Bitwise right shift
var a = 23>>4;// now a equal to 1
|
>>> |
Bitwise right shift with zero
var a = 0xff>>>4;// now a equal to 15
|
Logical
Operator | Description |
&& |
Logical and
if(a&&b){
...
}
|
|| |
Logical or
if(a||b){
...
}
|
! |
Logical not
if(!a){
...
}
|
Assigment
Operator | Description |
= |
Assign |
+= |
Add and assign
a+=10; // same as a = a + 10;
|
-= |
Subtract and assign |
*= |
Multiply and assign |
**= |
Exponentiate and assign
a**=3; // same as a = a *** 3;
|
/= |
Divide and assign. |
%= |
Take the remainder and assign |
Other operators
Operator | Description |
() |
Expression grouping or function call |
(?:) |
Conditional operator; returns value based on the condition like if-else.
// syntax
// expr ? val1 : val2
// if expr equal to true then return first value, otherwise second
var a= x>y ? 23 : 45 ;
|
, |
Allows multiple expressions to be evaluated as single statement.
var a=2, b=a+6;
|
new |
Create instance of object |
in |
Checks if object has the given property. |
instanceof |
Checks if the object is an instance of given type. |
typeof |
Checks the type of object. |
truthy and falsey values
As you know, in some cases JavaScript tries to convert the type.
In the case of boolean expressions, the following values are considered to be falsey:
- "" (empty string)
- 0
- null
- undefined
- NaN
Following values are considered to be truthy:
- {} (any object)
- [] (any array)
- "hello world" (non-empty string)
- 123 (non-zero number)
You can use double negation to convert a value to a boolean value.
console.log( typeof !!"hello world" === 'boolean'); // true