- Arithmetic operators
- String concatenation
- Increment/decrement:
++, --
- Bitwise operators
- Logical (boolean) operators
- Assignment operators
JavaScript has following basic operators…
Arithmetic operators
Basic arithmetic operators are: +, -, , /, %
.
You can use them with variables and values like this:
var i = 2; i = (2+i) * 3 / i; alert(i)
Just to note. Integers division returns a float. In contrast to java, where 7/2 = 3
(integer division), JavaScript will give you a normal 3.5
.
Modulo operator %
returns a remainder.
i = 5 % 2; // 1 i = 8 % 3; // 2 i = 6 % 3; // 0
String concatenation
Operator +
contatenates strings:
var a = "my" + "string" // mystring
If any of it’s arguments is a string, then another operand is coerced into a string too.
Unlike many other languages, there is no difference which argument is a string: the left one or the right one. In either case a non-string operand is converted.
For example:
alert( '1' + 2 ) // "12" alert( 2 + '1' ) // "21"
Increment/decrement: ++, --
These operators increment or decrement a variable by one.
Unlike other arithmetic operators, they change a variable itself.
var i = 3 i++ // increment to 4 alert(i) i-- // decrement to 3 alert(i)
You can also put ++
and --
before a variable (so-called *prefix form):
i = 3; ++i; // now 4 --i; // back to 3
Besides changing the variable, these operators return a value.
Which value is returned depends on the placement of ++(--)
.
- Postfix form:
i++
- Returns a value and then increments:
a = 2 alert(a++) // 2 alert(a) // 3
- Prefix form:
++i
- Increments and then returns the value:
a = 2; alert(++a) // 3
Bitwise operators
Bitwise operators treat numbers as signed 32-bit integers. If you are not familiar with them, read the general stuff from wikipedia Bitwise Operation article. They are rarely used, so you can also skip the section.
Following bitwise operators are supported:
- AND ( & )
- OR ( | )
- XOR ( ^ )
- NOT ( ~ )
- LEFT SHIFT ( << )
- RIGHT SHIFT ( >> )
- ZERO-FILL RIGHT SHIFT ( >>> )
Unlike C language, bitwise operations are not very fast in JavaScript: So they shouldn’t be used low-level optimizations.
Most of them are rarely used. Especially, the exotic zero-fill right shift. If you are interested, read the manual.
Smart integer operations
There are several tricks with bitwise operations to make the code shorter and faster.
That’s because a single bitwise operation can give the same result as several ordinary numerical operations.
The notable exception is bitwise NOT (~).
Bitwise NOT on an integer n
is same as -(n+1)
.
That is a side effect of inversing every bit of a number.
For example:
alert( ~1 ) // -2 alert( ~-1 ) // 0
Another use of binary operators is rounding. Because they treat numbers as integers, all of them cut off the decimal part.
For example, double bitwise NOT doesn’t change the integer, but cuts off the decimal part:
alert( ~~12.34 ) // 12
Other bitwise operators can be used to do the same:
alert( 12.9 ^ 0 ) // 12 alert( -13.5 << 0 ) // 13
Regular division by 2 may return a floating point number. But binary operation always returns integer.
The right shift a >> b
operator is actually same as a/2b
.
See examples:
alert( 5 >> 1 ) // integer division without a remainder: 5 / 2 = 2 alert( 21 >> 2 ) // 21 / 4 = 5 alert( 21 >> 3 ) // 21 / 8 = 2 alert( 21 >> 4 ) // 21 / 16 = 1
Logical (boolean) operators
Like many other languages, JavaScript provides boolean evaluations.
The operators are:
- Logical AND ( && )
- Logical OR ( || )
- Logical NOT ( ! )
Unlike bitwise operators, logical AND, OR, NOT operate with boolean values.
alert( true && false ) // false alert( false || true ) // true alert( !false ) // true
In JavaScript, logical operators can be applied to anything. A non-boolean value is converted into boolean for the purposes of evaluation.
There is a short list of falsy values, which are 0, empty string ” and few special values such as null
and undefined
. All other values are true
.
Logical evaluations are short-circuit. In other words, they never evaluate more than needed.
The result is the value which evaluated last.
For example, operator OR:
// Left operand is true, // OR stops and returns it immediately alert(1 || 0) // *!*1*/!*, the last evaluated value // Left operand is false, // the evaluation goes to the right operand alert(0 || 1) // *!*1*/!*, the last evaluated value
What if both operands are falsy?
// Left operand is false, // the evaluation goes to the right operand alert('' || null) // *!*null*/!*, the last evaluated value
Logical OR is useful when you have multiple variables and need to get the first one which is true:
var b = false var n = 0 var s = "O-la-la" var result = b || n || s alert(result) // "O-la-la"
Operator AND is similar:
// Left operand is true, // AND needs to check the right operand also alert(1 && 0) // *!*0*/!*, the last evaluated value alert(1 && 5) // *!*5*/!*, the last evaluated value // Left operand is false, // AND stops immediately and returns it alert(null && 5) // *!*null*/!*, the last evaluated value
As of NOT operator "!"
, it converts its operand into boolean and negates it. The result is always a boolean value.
alert( !true ) // false alert( !0 ) // true
Double NOT is used to convert a value into boolean:
alert( !!"string" ) // true alert( !!null ) // false
Assignment operators
Most operators may be used as assignment.
The list is: =, +=, -=, *=, /=, >>=, <<=, >>>=, &=, |=, ^=
.
A usage pattern may look like:
i = 2; i += 5; // 7 i *= 2; // 14
Note, boolean operators can’t be used like that. For example, a ||= b
will give error, because there is no operator "||="
(check the list above).
There is an “advanced” JavaScript operator, named the comma operator
.
The comma operator allows to list statements delimited by “,” and returns the value of the last one.
The following syntax is a valid comma operator example:
a = (5, 6) alert(a)
Brackets are put for correct priority, because “,” usually goes after assignment. So, the code without brackets would evaluate as (a=5),6
.
There is no real need in comma operator, but it allows to put multiple statements in one line and made the code shorter. Like this:
for(var i=0, j=obj.length; i<j; i++ ) { // ... }
In the example above, j=obj.length
is merged with the loop start to evade extra line.