Expressions & Operators - Web Docs
Operator | Description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
** | Exponential |
/ | Division |
% | Modulus (Division Remainder) |
++ | Increment |
– | Decrement |
Operator | Example | Same As |
---|---|---|
= | x = y | x = y |
+= | x += y | x = x + y |
-= | x -= y | x = x - y |
*= | x *= y | x = x * y |
/= x | /= y | x = x / y |
%= | x %= y | x = x % y |
**= x | **= y | x = x ** y |
+
operator can also be used to concatenate stringslet text1 = "John";
let text2 = "Doe";
let text3 = text1 + " " + text2;
// output of text3 = 'John Doe'
Operator | Description |
---|---|
== | equal to |
=== | equal value and equal type |
!= | not equal |
!== | not equal value or not equal type |
> | greater than |
< | less than |
>= | greater than or equal to |
<= | less than or equal to |
? | ternary operator |
Operator | Description |
---|---|
&& | logical and |
|| |
logical or |
! | logical not |
Operator | Description |
---|---|
typeof | Returns the type of a variable |
instanceof | Returns true if an object is an instance of an object type |
Operator | Description | Example | Same As | Result | Decimal |
---|---|---|---|---|---|
& | AND | 5 & 1 | 0101 & 0001 | 0001 | 1 |
| |
OR | 5 | 1 |
0101 | 0001 |
0101 | 5 |
~ | NOT | ~ 5 | ~0101 | 1010 | 10 |
^ | XOR | 5 ^ 1 | 0101 ^ 0001 | 0100 | 4 |
« | left shift | 5 « 1 | 0101 « 1 | 1010 | 10 |
» | right shift | 5 » 1 | 0101 » 1 | 0010 | 2 |
»> | unsigned right shift | 5 »> 1 | 0101 »> 1 | 0010 | 2 |
for
loop repeats until a specified condition evaluates to false
Syntax:
for ([initialExpression]; [conditionExpression]; [incrementExpression])
statement
Example:
let countDown = 10 // Set initial condition in variable
for (let i = countDown; i >= 0; i--) // While i >= 0, decrement by 1, then exit the loop
{
console.log(`${i} second${i !== 1 ? 's' : ''} until lift off!`) // Logs count of i
}
console.log('Lift Off!') // When i < 0, log 'Lift Off!'
do...while
statement repeats until a specified condition evaluates to false
Syntax:
do
statement
while (condition);
Example:
let i = 0; // Set initial condition
do { // Do the statement below
i += 1;
console.log(i);
} while (i < 5); // While i is < 5
while
statement executes its statements as long as a specified condition is true
condition
becomes false
, statement
within the loop stops executing and control passes to the statement that follows the loop
statement
is the loop is executed
true
the loop continues, if false
, the loop ends and the program moves on to the next block of codeSyntax:
while (condition)
statement
Example:
let i = 10
while (i >= 0){ // While i is >= 0, the statement will execute
console.log(`Countdown: ${i}`) // First log value of i
i-- // Then decrement i by 1
}
console.log('Blast off!') // When i drops below 0, log 'Blast Off'