Having explained operator precedence, associativity, and other background material, we can start to describe the operators themselves. This section details the arithmetic operators.
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.
The - operator subtracts its second operand from its first. Both operands must be numbers. Used as a unary operator, - negates its operand.
The * operator multiplies its two operands, which must both be numbers.
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.
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.
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.
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;
i =index.html 1; j = i++;
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.
file: /Techref/language/JAVA/SCRIPT/definitive/ch04_03.htm, 7KB, , updated: 2019/10/14 15:00, local time: 2024/12/19 02:41,
3.133.124.48: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? <A HREF="http://massmind.org/techref/language/JAVA/SCRIPT/definitive/ch04_03.htm"> [Chapter 4] 4.3 Arithmetic Operators</A> |
Did you find what you needed? |
Welcome to massmind.org! |
Welcome to massmind.org! |
.