HTML and CSS Reference

In-Depth Information

Figure 5.2
Output from Example 5.2: Precedence and associativity.

5.2

Types of Operators

Arithmetic operators take numerical values (either literals or variables) as their oper-

ands and return a single numerical value. The standard arithmetic operators are addition

(+), subtraction (-), multiplication (*), and division (/). See Table 5.2.

Table 5.2
Arithmetic Operators

Operator/Operands

Function

x + y

Addition

x - y

Subtraction

x * y

Multiplication

Division
a

x / y

x % y

Modulus

a. The / operator returns a floating-point division in JavaScript, not a truncated division as it does in languages

such as C or Java. For example, 1/2 returns 0.5 in JavaScript and 1/2 returns 0 in Java.

EXAMPLE
5.3

<html>

<head><title>Arithmetic Operators</title></head>

<body>

<h2>Arithmetic operators</h2>

<h3>