Assignments and Comparisons

When you put a value into a variable, you are assigning that value to the variable, and you use an assignment operator to do the job. For example, you use the equals operator to make an assignment, such as hisName = "Tom". There are a whole set of assignment operators as listed in Table 1.4.

Table 1.4. Assignments
AssignmentWhat it does
x = ySets x to the value of y
x += ySame as x = x + y
x -= ySame as x = x - y
x *= ySame as x = x * y
x /= ySame as x = x / y
x %= ySame as x = x % y

Other than the equals sign, the other assignment operators serve as shortcuts for modifying the value of variables. For example, a shorter way to say x=x+5 is to say x+=5. For the most part, we’ve used the longer version in this book for clarity’s ...

Get JavaScript and Ajax for the Web: Visual QuickStart Guide, Seventh Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.