AJAX - The Complete Reference

(avery) #1

PART IV


Appendix A: JavaScript Quick Reference 569


Arithmetic Operators


Arithmetic operators operate solely on numbers, with one exception, +, which is overloaded
and provides string concatenation as well. Table A-12 details the arithmetic operators found
in JavaScript.

Bitwise Operators


While JavaScript does not allow for standard C-like memory access, it does include bitwise
operators. Bitwise operators operate upon integers in a bit-by-bit fashion. Most computers store
negative numbers using their two’s complement representation, so you should exercise caution
when performing bit operations on negative numbers. Most uses of JavaScript rarely involve
bitwise operators, but they are presented in Table A-13 for those so inclined to use them.

Assignment Operators


Assigning a value to variable is performed using the = operator. There are a number of
shorthand notations in JavaScript that allow you to perform simple arithmetic or bitwise
operations and assign at the same time. These operators are shown in Table A-14.

Operator Operation Example

+ (unary) Has no effect on numbers but
causes non-numbers to be
converted into numbers

var x = +5;
var y = +"10";
// converted to 10


  • (unary) Negation (changes the sign of
    the number or converts the
    expression to a number and
    then changes its sign)


var x = -10;

+ Addition (also functions as string
concatenation)

var sum = 5 + 8; // 13


  • Subtraction var difference = 10 - 2; // 8



  • Multiplication var product = 5 * 5; // 25


/ Division var result = 20 / 3; //
6.6666667
% Modulus (the remainder when
the first operand is divided by
the second)

alert(9.5 % 2); // 1.5

++ Auto-increment (increment the
value by one and store); may be
prefixed or postfixed but not both

var x = 5;
x++; // x now 6

−− Auto-decrement (decrement the
value by one and store); may be
prefixed or postfixed but not both

var x = 5;
x--; // x now 4

TABLE A-12 Arithmetic Operators
Free download pdf