# Arithmetic - JavaScript(JS)

Many scripts perform arithmetic calculations.

Arithmetic

Many scripts perform arithmetic calculations. Figure 6.13 summarizes the arithmetic op-erators. Note the use of various special symbols not used in algebra. The asterisk (*) indi-cates multiplication; the percent sign (%) is the remainder operator, which will be discussed shortly. The arithmetic operators in Fig. 6.13 are binary operators, because each operates on two operands. For example, the expression sum + value contains the binary operator + and the two operands sum and value.

JavaScript provides the remainder operator, %, which yields the remainder after divi-sion. [Note: The % operator is known as the modulus operator in some programming lan-guages.] The expression x % y yields the remainder after x is divided by y. Thus, 17 % 5 yields 2 (i.e., 17 divided by 5 is 3, with a remainder of 2), and 7.4 % 3.1 yields 1.2. In later chapters, we consider applications of the remainder operator, such as determining whether one number is a multiple of another. There is no arithmetic operator for exponentiation in JavaScript. (Chapter 8, JavaScript: Control Statements II, shows how to perform expo-nentiation in JavaScript using the Math object’s pow method.)

Arithmetic expressions in JavaScript must be written in straight-line form to facilitate entering programs into the computer. Thus, expressions such as “a divided by b” must be written as a / b, so that all constants, variables and operators appear in a straight line. The following algebraic notation is generally not acceptable to computers:

Parentheses are used to group expressions in the same manner as in algebraic expres-sions. For example, to multiply a times the quantity b + c we write:

a * ( b + c )

JavaScript applies the operators in arithmetic expressions in a precise sequence deter-mined by the following rules of operator precedence, which are generally the same as those followed in algebra:

1. Multiplication, division and remainder operations are applied first. If an expres-sion contains several multiplication, division and remainder operations, opera-tors are applied from left to right. Multiplication, division and remainder operations are said to have the same level of precedence.

2. Addition and subtraction operations are applied next. If an expression contains several addition and subtraction operations, operators are applied from left to right. Addition and subtraction operations have the same level of precedence.

The rules of operator precedence enable JavaScript to apply operators in the correct order. When we say that operators are applied from left to right, we are referring to the associa-tivity of the operators—the order in which operators of equal priority are evaluated. We will see that some operators associate from right to left. Figure 6.14 summarizes the rules of operator precedence. The table in Fig. 6.14 will be expanded as additional JavaScript operators are introduced. A complete precedence chart is included in Appendix C.

Now, in light of the rules of operator precedence, let us consider several algebraic expressions. Each example lists an algebraic expression and the equivalent JavaScript expression.

The following is an example of an arithmetic mean (average) of five terms:

The parentheses are required to group the addition operators, because division has higher precedence than addition. The entire quantity ( a + b + c + d + e ) is to be divided by 5. If the parentheses are erroneously omitted, we obtain a + b + c + d + e / 5, which evaluates as

and would not lead to the correct answer.

The following is an example of the equation of a straight line:

No parentheses are required. The multiplication operator is applied first, because multi-plication has a higher precedence than addition. The assignment occurs last, because it has a lower precedence than multiplication and addition.

The following example contains remainder (%), multiplication, division, addition and subtraction operations:

The circled numbers under the statement indicate the order in which JavaScript applies the operators. The multiplication, remainder and division operations are evaluated first in left-to-right order (i.e., they associate from left to right), because they have higher prece-dence than addition and subtraction. The addition and subtraction operations are evalu-ated next. These operations are also applied from left to right.

To develop a better understanding of the rules of operator precedence, consider the evaluation of a second-degree polynomial (y = ax2 + bx + c):

The circled numbers indicate the order in which JavaScript applies the operators. Suppose that a, b, c and x are initialized as follows: a = 2, b = 3, c = 7 and x = 5.

Figure 6.15 illustrates the order in which the operators are applied in the preceding second-degree polynomial.

As in algebra, it is acceptable to use unnecessary parentheses in an expression to make the expression clearer. These are also called redundant parentheses. For example, the pre-ceding second-degree polynomial might be parenthesized as follows:

y = ( a * x * x ) + ( b * x ) + c;

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Related Topics

Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.