! " $ % & ' ( ) * + , - . / : ; < = > ? [ \ ] ^ _ { | } ~

! exclamation point

! prefix operator not

The ! operator produces false if its operand is truthy, and true if its operand is falsy.

! true        // false
! false       // true
! 'false'     // false
! 0           // true
! 42          // false
!! 42         // true 

!= infix operator coercing not equal

The != operator determines if two operands are not equal. If the operands have different types it may attempt to coerce the types of the operands before comparing them. It produces the opposite of the == infix operator.

The != operator produces the same result as this function:

function coercing_not_equal(left, right) {
    return !(left == right);
}

Avoid this operator. Always use the !== infix operator instead.

!== infix operator not equal

The !== operator determines if two operands are not equal. It produces the opposite of the === infix operator. The result is false only if the types of the operands are the same.

If the operands are objects, the result is false only if the operands are the same object. If the operands are similar objects, the result is true.

The != operator produces the same result as this function:

function not_equal(left, right) {
    return !(left === right);
}

The expression x !== x will always result in false except when the value of x is NaN. This bizarre behavior is intentional.

false != 'false'             // true
1 !== '1'                    // true
null !== undefined           // true
{} !== {}                    // true
1 !== 1.0                    // false
'cat' !== 'c' + 'a' + 't'    // false
NaN !== NaN                  // true

" double quotation mark

See string literal.

$ dollar sign

$ identifier character

The $ character is treated as a letter in an identifier. It was added to the language specifically for the use of code generators and macro processors so that they could generate identifiers with confidence that they would not collide with programs produced by humans. Unfortunately, the developer community ignored the intention of $ and used it to create ugly, cryptic identifiers that collide with each other.

ES5 does not attempt to discourage the use of $.

% percent sign

% infix operator remainder

The % operator computes the remainder produced by the division of two operands, a dividend and a divisor. If the operands are not numbers, it attempts to coerce them to numbers. If either operand cannot be coerced to a number or is NaN, or if the dividend is an Infinity, or if the divisor is a zero, then the result is NaN. Otherwise, if the divisor is an Infinity, the result is the dividend.

The % operator produces the same result as this function:

function remainder(left, right) {
    var dividend = +left,
        divisor = +right,
        quotient = dividend / divisor;
    if (divisor === Infinity || divisor === -Infinity) {
        return isFinite(dividend) ? dividend : NaN;
    } else {
        return dividend - (divisor * Math[quotient >= 0 ?
                'floor' : 'ceil'](quotient));
    }
}

The sign of the result will be the sign of the dividend, not the divisor. This differs from the modulo operator found in some other languages, where the result has the sign of the divisor. The modulo function can be computed with this function, which adds the divisor to the remainder if the remainder has the wrong sign:

function modulo(left, right) {
    var dividend = +left,
        divisor = +right,
        mod = dividend % divisor;
    if (mod && (divisor > 0) === (mod < 0)) {
        mod += divisor;
    }
    return mod;
}

Examples:

 15 %  4            //  3
-15 %  4            // -3
-15 % -4            // -3
 15 % -4            //  3
 15 %  0            // NaN
 15 % -Infinity     // 15
  0 %  Infinity     //  0
  0 %  0            // NaN
Infinity % 4        // NaN
Infinity % Infinity // NaN

%= assignment infix operator remainder

The %= operator replaces the left value with the remainder of the left value divided by the right value. So

left %= divisor;

is a streamlined equivalent of

left = left % divisor;

& ampersand

& infix operator bitwise and

The & infix operator performs the bitwise and operation. The and operation uses 32 bits from each operand. If two corresponding bits are 1, then the corresponding result bit is 1. Otherwise, the corresponding result bit is 0. The operands are converted from a number (64 bit floating point) to a 32-bit integer. It produces the same result as this function:

function bitwise_and(left, right) {
    var result = 0, bit, i, left_int, right_int,
        two_32 = 4294967296,    // Math.pow(2, 32)
        two_31 = 2147483648;    // Math.pow(2, 31)

// Make 32-bit unsigned integers from the operands. The % is a remainder
// operator, not a modulo operator, so some sign correction may be necessary.

    left_int = Math[left >= 0 ? 'floor' : 'ceil'](left) % two_32;
    if (left_int < 0) {
        left_int += two_32;
    }
    right_int = Math[right >= 0 ? 'floor' : 'ceil'](right) % two_32;
    if (right_int < 0) {
        right_int += two_32;
    }

// For each of the 32 bits,
//      Double the current result
//      Set the new bit to 1
//      If the most significant bits are set, then subtract them out
//      Otherwise clear the new bit
//      Add the new bit to the result
//      Double the ints, rotating the next most significant bit into position

    for (i = 0; i < 32; i += 1) {
        result += result;
        bit = 1;
        if (left_int >= two_31) {
            left_int -= two_31;
        } else {
            bit = 0;
        }
        if (right_int >= two_31) {
            right_int -= two_31;
        } else {
            bit = 0;
        }
        result += bit;
        left_int += left_int;
        right_int += right_int;
    }

// If the most significant bit of the result is set, then produce a negative.

    if (result >= two_31) {
        result -= two_32;
    }
    return result;
}

&& infix operator and

Blah.

&= assignment infix operator bitwise and

Blah.

' single quotation mark

' string delimiter

See string literal.

( left parenthesis

Parentheses have many uses. In operand position, they provide grouping for overriding operator precedence or to make the meaning of complicated expressions clearer. In suffix position, they cause the invocation of functions. In function definitions and catch blocks they define parameters and exception variables. In statement position, they can disambiguate function literal from function statement. In a statement they denote a conditional expression or a special variable.

( ) grouping

In expressions, parentheses can be used to override the precedence of operators, or to break up complicated expressions to make them easier to read.

3 + 4 * 5      // 23
3 + (4 * 5)    // 23
(3 + 4) * 5    // 35 

Parentheses can also be used to improve the readability of an immediate function.

( ) parameter list

See function.

( ) statement punctuator

Parentheses are used in statements. See

( ) suffix operator invoke

Blah.

) right parenthesis

See ( left parenthesis.

* asterisk

* infix operator multiply

The * operator multiplies its operands together. If the operands are not numbers, it attempts to coerce them to numbers. If either operand cannot be coerced to a number or is NaN, then the result will be NaN.

3 * 4    // 7

*/ Close comment

See /*.

*= assignment infix operator

Blah.

+ plus sign

+ infix operator add or concatenate

The + operator can either add or concatenate, depending of the types of its operands. If either operand is a string, then it converts both operands to strings and concatenates then, producing a new string with all of the characters from both operands. Otherwise, it converts both operands to numbers and then attempts to add them. It produces the same result as this function:

function add_or_concatenate(left, right) {
    if (left && typeof left === 'object') {
        left = left.valueOf();
    }
    if (right && typeof right === 'object') {
        right = right.valueOf();
    }
    if (typeof left === 'string' || typeof right === 'string') {
        return String(left).concat(String(right));
    }
    return +left + (+right);    // return the sum of left + right
}

The + operator is the only arithmetic operator with this type confusion.

Examples:

1 + 0                  // 1
1 + 1                  // 2
1 + (-1)               // 0
1 + ''                 // '1'
1 + undefined          // NaN
1 + null               // 1
1 + false              // 1
1 + true               // 2
1 + NaN                // NaN
1 + Infinity           // Infinity
true + true            // 2
true + false           // 1
true + '!'             // 'true!'
Infinity + (-1)        // Infinity
Infinity + Infinity    // Infinity
'' + ''                // ''
'1' + "1"              // '11'
'1' + 0                // '10'
'1' + ''               // '1'
'1' + undefined        // '1undefined'
'1' + null             // '1null'
'1' + NaN              // '1NaN'
'1' + Infinity         // '1Infinity'
'1' + true             // '1true'
'$' + 3 + 4            // '$34'
'$' + (3 + 4)          // '$7'

+ prefix operator to number

Blah.

++ assignment prefix operator pre-increment

Blah.

++ assignment suffix operator post-increment

Blah.

+= assignment infix operator add or concatenate

Blah.

, comma

, infix operator

The , infix operator takes two operands, evalutes them both, and returns the value of the second operand. This operator may not be used in places where a comma can appear, such as in an argument list, in a var statement, or in an object literal or array literal unless the operand it forms is wrapped in grouping parentheses. This operator is marginally useful in the for statement when initializing or incrementing multiple variables.

, separator

The comma is used to separate operands in the object literal, the array literal, the argument list. The comma is used to separate identifiers in the the parameter list and the var statement.

- minus

- infix operator subtract

Blah.

- prefix operator negate

Blah.

-- assignment prefix operator pre-decrement

Blah.

-- assignment suffix operator post-decrement

Blah.

-= assignment infix operator subtract

Blah.

. decimal point

Blah.

. suffix operator select

Blah.

/ slash

The / slash character is also known as the solidus and as the virgule. It is used as the division operator, and it is used to form comments and regexp literals. Be careful to not confuse it with \ backslash.

/ infix operator divide

Blah.

/ regexp delimiter

Blah.

/* comment */

Blah.

// comment

Blah.

/= assignment infix operator divide

Blah.

: colon

: pair separator

See object literal.

: label

See label.

: switch statement case terminator

See switch statement.

: ternary operator

See ? ternary operator.

; semicolon

The ; is used to terminate a statement. It is required on these statements:

In some cases a mechanism called semicolon insertion allows for leaving off the terminating semicolon. Unfortunately, semicolon insertion has some inherent problems and should not be relied upon.

Semicolon is also used to separate the control clauses in a for statement.

< left angle bracket

< infix operator less than

Blah.

<< infix operator bitwise shift left

Blah.

<<= assignment infix operator bitwise shift left

Blah.

<= infix operator less than or equal

 

= equal sign

= assignment infix operator

Blah.

== infix operator coercing equal

The == operator determines if two operands are equal. If the operands have different types it may attempt to coerce the types of the operands before comparing them. It produces the opposite of the != infix operator.

The == operator produces the same result as this function:

function coercing_equal(left, right) {
    if (left === right) {
        return true;
    }
    if (left === null) {
        return right === undefined;
    }
    if (right === null) {
        return left === undefined;
    }
    if (typeof left === 'number' && typeof right === 'string') {
        return left === +right;
    }
    if (typeof left === 'string' && typeof right === 'number') {
        return +left === right;
    }
    if (typeof left === 'boolean') {
        return coercing_equal(+left, right);
    }
    if (typeof right === 'boolean') {
        return coercing_equal(left, +right);
    }
    if (typeof left === 'object' &&
            (left.constructor === Number || left.constructor === String ||
                left.constructor === Boolean) &&
            (typeof right === 'string' || typeof right === 'number')) {
        return coercing_equal(left.valueOf(), right);
    }
    if ((typeof left === 'string' || typeof left === 'number') &&
            typeof right === 'object' &&
            (right.constructor === Number || right.constructor === String ||
                right.constructor === Boolean)) {
        return coercing_equal(left, right.valueOf());
    }
    return false;
}

Avoid this operator. Always use the === infix operator instead.

Examples:

'' == 0                  // false
0 == ''                  // true
0 == '0'                 // true
false == 'false'         // false
false == 0               // true
false == undefined       // false
false == null            // false
null == undefined        // true
' \t\r\n ' == 0          // true
'cat' === "cat"          // true
'cat' === 'CAT'          // false
Infinity == Infinity     // true
-Infinity == Infinity    // false
Infinity == NaN          // false
NaN == NaN               // false

=== infix operator equal

The === operator compares two values, producing true if they are equal and false if they are not. Two strings are considered equal if they have the same length and contain exactly the same code points in the same sequence. To references to the same object or array are considered equal. Two similar objects or two similar arrays are not considered equal.

The expression x === x will always result in true except when the value of x is NaN. Surprisingly, there exist mathematicians that think this was a good idea. It presents an obvious programming hazard. For example, suppose you need a function that tests that a function returns the correct value. Special handling is required for the case where the expected value is NaN. Simply using === to compare the expected value and the result will do the wrong thing when the expected value is NaN, so much more complicated comparisons are required using the typeof prefix operator and the isNaN global function. (The isNaN global function can produce incorrect results when its argument is a string.)

Two numbers that are only approximately equal may produce a false result.

Examples:

'' === 0                  // false
0 === ''                  // false
0 === '0'                 // false
false === 'false'         // false
false === 0               // false
false === undefined       // false
false === null            // false
null === undefined        // false
' \t\r\n ' === 0          // false
Infinity === Infinity     // true
-Infinity === Infinity    // false
Infinity === NaN          // false
NaN === NaN               // false
0 === 0.00                // true
0 === -0                  // true
{} === {}                 // false
[] === []                 // false
0.1 + 0.2 === 0.3         // false

> right angle bracket

> infix operator greater than

Blah.

>= infix operator greater than or equal

Blah.

>> infix operator bitwise signed shift right

Blah.

>>= assignment infix operator bitwise signed shift right

Blah.

>>> infix operator bitwise unsigned shift right

Blah.

>>>= assignment infix operator bitwise unsigned shift right

Blah.

? question mark

? ternary operator conditional

The ? operator takes three operands which are separated by the ? and a :.

condition ? then : else

It is a short-circuiting operator in that either the then operand or the else operand will not be evaluated. If the condition operand is truthy then the then operand will be evaluated and its value will be the value of the expression. If the condition operand is falsy then the else operand will be evaluated and its value will be the value of the expression.

function signum(number) {

// Return 0 if the number is zero,
//        1 if the number is positive,
//       -1 if the number is negative.

    return number === 0 ? 0 : number > 0 ? 1 : -1;
}

[ left bracket

[ ] array literal

See array literal.

[ ] suffix operator subscript

Blah.

\ backslash

\ identifier escapement

Blah.

\ string escapement

Blah.

] right bracket

See [ left bracket.

^caret

^ infix operator bitwise exclusive or

Blah.

^= assignment infix operator bitwise exclusive or

Blah.

_ underbar

In a name, the _ underbar character is treated as a letter. The _ underbar is used to improve the readability of names, especially when the name contains several words, which as

calculate_the_total

since a name cannot contain a space or hyphen. Another convention is to use camelCase:

calculateTheTotal

Some implementations use leading or trailing _ underbar for internal or dangerous properties, so it is best to avoid such names.

{ left curly brace

{ } block statement

See block.

{ } function body

See function.

{ } object literal

See object literal.

| vertical bar

| infix operator bitwise or

The | operator performs the bitwise or operation. The or operation uses 32 bits from each operand. If two corresponding bits are 0, then the corresponding result bit is 0. Otherwise, the corresponding result bit is 1. The operands are converted from a number (64 bit floating point) to a 32-bit integer.

function bitwise_or(left, right) {
    var result = 0, bit, i, left_int, right_int,
        two_32 = Math.pow(2, 32),    // 4294967296
        two_31 = Math.pow(2, 31);    // 2147483648

// Make 32-bit unsigned integers from the operands. The % is a remainder
// operator, not a modulo operator, so some sign correction may be necessary.

    left_int = Math[left >= 0 ? 'floor' : 'ceil'](left) % two_32;
    if (left_int < 0) {
        left_int += two_32;
    }
    right_int = Math[right >= 0 ? 'floor' : 'ceil'](right) % two_32;
    if (right_int < 0) {
        right_int += two_32;
    }

// For each of the 32 bits,
//      Double the current result
//      Set the new bit to 0
//      If the most significant bits are set, then subtract out the bits out
//          and set the new bit
//      Add the new bit to the result
//      Double the ints, rotating the next most significant bit into position

    for (i = 0; i < 32; i += 1) {
        result += result;
        bit = 0;
        if (left_int >= two_31) {
            left_int -= two_31;
            bit = 1;
        }
        if (right_int >= two_31) {
            right_int -= two_31;
            bit = 1;
        }
        result += bit;
        left_int += left_int;
        right_int += right_int;
    }

// If the most significant bit of the result is set, then produce a negative.

    if (result >= two_31) {
        result -= two_32;
    }
    return result;
}

|= assignment infix operator bitwise or

Blah.

|| infix operator or

Blah.

} right curly brace

See { left curly brace.

~ tilde

~ prefix operator bitwise not