please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 4
Expressions and Operators
Next
 

4.3 Arithmetic Operators

Having explained operator precedence, associativity, and other background material, we can start to describe the operators themselves. This section details the arithmetic operators.

Addition (+)

The + operator adds its two numeric operands. If both operands are strings, then it returns a string that is the result of concatenating the second operand onto the first. If either operand is a string, then the other is converted to a string, and the two strings are concatenated. Furthermore, if either operand is an object, then both operands are converted to strings and concatenated.

Subtraction (-)

The - operator subtracts its second operand from its first. Both operands must be numbers. Used as a unary operator, - negates its operand.

Multiplication (*)

The * operator multiplies its two operands, which must both be numbers.

Division (/)

The / operator divides its first operand by its second. Both operands must be numbers. If you are a C programmer, you might expect to get an integer result when you divide one integer by another. In JavaScript, however, all numbers are floating-point, so all divisions have floating-point results: 5/2 evaluates to 2.5, not 2.

Modulo (%)

The % operator computes the first operand modulo the second operand. That is, it returns the remainder when the first operand is divided by the second operand an integer number of times. Both operands must be numbers. For example, 5 % 2 evaluates to 1.

While the modulo operator is typically used with integer operands, it also works for floating-point values. For example, 4.3 % 2.1 ==index.html 0.1.

Unary Negation (-)

When - is used as a unary operator, before a single operand, it performs unary negation, i.e., it converts a positive value to an equivalently negative value, and vice versa.

Increment (++)

The ++ operator increments (i.e., adds 1 to) its single operand, which must be a variable, an element of an array, or a property of an object that refers to a numeric value. The precise behavior of this operator depends on its position relative to the operand. When used before the operand, where it is known as the pre-increment operator, it increments the operand and evaluates to the incremented value of that operand. When used after the operand, where it is known as the post-increment operator, it increments its operand, but evaluates to the unincremented value of that operand.

For example, the following code sets both i and j to 2:

i =index.html 1;
j = ++i;
But these lines set i to 2 and j to 1:

i =index.html 1;
j = i++;
This operator, in both its forms, is most commonly used to increment a counter that controls a loop.

Decrement (--)

The -- operator decrements (i.e., subtracts 1 from) its single numeric operand, which must be a variable, an element of an array, or a property of an object. Like the ++ operator, the precise behavior of -- depends on its position relative to the operand. When used before the operand, it decrements and returns the decremented value. When used after the operand, it decrements, but returns the undecremented value.


Previous Home Next
Operator Overview Book Index Comparison Operators

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell

file: /Techref/language/JAVA/SCRIPT/definitive/ch04_03.htm, 7KB, , updated: 2019/10/14 15:00, local time: 2024/12/29 05:30,
TOP NEW HELP FIND: 
3.22.68.29:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://linistepper.com/Techref/language/JAVA/SCRIPT/definitive/ch04_03.htm"> [Chapter 4] 4.3 Arithmetic Operators</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?