mozilla
Your Search Results

    Arithmetic Operators Redirect 1

    Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value. The standard arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/).

    These operators work as they do in most other programming languages when used with floating point numbers (in particular, note that division by zero produces +/-Infinity). For example:

    1 / 2      // returns 0.5 in JavaScript
    1 / 2      // returns 0 in Java (neither number is explicitly a floating point number)
    
    1.0 / 2.0  // returns 0.5 in both JavaScript and Java
    
    2.0 / 0    // returns Infinity in Javascript
    2.0 / 0.0  // returns Infinity too
    2.0 / -0.0 // returns -Infinity in Javascript
    
    
    Operators
    Implemented in: JavaScript 1.0
    ECMA Version: ECMA-262

     % (Remainder)

    The remainder operator is used as follows:

    var1 % var2

    The remainder operator returns the first operand modulo the second operand, that is, var1 modulo var2, in the preceding statement, where var1 and var2 are variables. The modulo function is the integer remainder of dividing var1 by var2. For example, 12 % 5 returns 2. The result will have the same sign as var1; that is, -1 % 2 returns -1.

    There is a proposal to get an actual modulo operator in a future version of ECMAScript

    ++ (Increment)

    The increment operator is used as follows:

    var++ or ++var

    This operator increments (adds one to) its operand and returns a value. If used postfix, with operator after operand (for example, x++), then it returns the value before incrementing. If used prefix with operator before operand (for example, ++x), then it returns the value after incrementing.

    For example, if x is three, then the statement y = x++ sets y to 3 and increments x to 4. If x is 3, then the statement y = ++x increments x to 4 and sets y to 4.

    -- (Decrement)

    The decrement operator is used as follows:

    var-- or --var

    This operator decrements (subtracts one from) its operand and returns a value. If used postfix (for example, x--), then it returns the value before decrementing. If used prefix (for example, --x), then it returns the value after decrementing.

    For example, if x is three, then the statement y = x-- sets y to 3 and decrements x to 2. If x is 3, then the statement y = --x decrements x to 2 and sets y to 2.

    - (Unary Negation)

    The unary negation operator precedes its operand and negates it. For example, y = -x negates the value of x and assigns that to y; that is, if x were 3, y would get the value -3 and x would retain the value 3.

    + (Unary Plus)

    The unary plus operator precedes its operand and evaluates to its operand but attempts to converts it into a number, if it isn't already. For example, y = +x takes the value of x and assigns that to y; that is, if x were 3, y would get the value 3 and x would retain the value 3; but if x were the string "3", y would also get the value 3. Although unary negation (-) also can convert non-numbers, unary plus is the fastest and preferred way of converting something into a number, because it does not perform any other operations on the number. It can convert string representations of integers and floats, as well as the non-string values true, false, and null. Integers in both decimal and hexadecimal ("0x"-prefixed) formats are supported. Negative numbers are supported (though not for hex). If it cannot parse a particular value, it will evaluate to NaN.

    Document Tags and Contributors

    Contributors to this page: Sheppy
    Last updated by: Sheppy,