HTML and CSS Reference
In-Depth Information
chapter
Operators
5.1 About JavaScript Operators
and Expressions
Data objects can be manipulated in a number of ways by the large number of operators
provided by JavaScript. Operators are symbols, such as +, -, =, >, and <, that produce a
result based on some rules. An operator manipulates data objects called operands; for
example, 5 and 4 are operands in the expression 5 + 4 . Operators and operands are found
in expressions. An expression combines a group of values to make a new value, n = 5 + 4 .
And when you terminate an expression with a semicolon or a newline, you have a com-
plete statement (e.g., n = 5 + 4; ).
var sum
=
5
+
4
new variable
operator
operand
operator
operand
In the numeric expression , 5 + 4 - 2 , three numbers are combined. The operators are
the + and - signs. The operands for the + sign are 5 and 4 . After that part of the expres-
sion is evaluated to 9 , the expression becomes 9 - 2 . After evaluating the complete
expression, the result is 7 . Because the plus and minus operators each manipulate two
operands, they are called binary operators. If there is only one operand, the operator is
called a unary operator. If there are three operands, it is called a ternary operator. We'll
see examples of these operators later in the chapter.
The operands can be either strings, numbers, Booleans, or a combination of these. We
have already used some of the operators: the concatenation operator to join two strings
together, the typeof operator to determine what data type is being used, and the assign-
ment operator used to assign a value to a variable. Now let's look at a plethora of addi-
tional JavaScript operators and see how they manipulate their operands.
83
 
 
 
 
Search WWH ::




Custom Search