### Values and Variables

In JavaScript, a piece of information is a value. There are different kinds of values; the kind you're most familiar with are numbers. A string value is a word or words enclosed in quotes. Other kinds of JavaScript values are listed in Table 1.2.

##### Table 1.2. Value Types

Type

Description

Example

Number

Any numeric value

3.141592654

String

Characters inside quote marks

"Hello, world!"

Boolean

True or False

true

Null

Empty and meaningless

Object

Any value associated with the object

Function

Value returned by a function

Variables contain values. For example, the variable myName is assigned the string "Dori". Another way to write this is myName="Dori". The equals sign can be read as "is set to." In other words, the variable myName now contains the value "Dori".

Tips

• JavaScript is case sensitive. This means that myname is not the same as myName, and neither is the same as MyName.

• Variable names cannot contain spaces or other punctuation, or start with a digit. They also can't be one of the JavaScript reserved words. See Appendix B for a list of JavaScript reserved words.

#### Operators

Operators are the symbols used to work with variables. You're already familiar with operators from simple arithmetic; plus and minus are operators. See Table 1.3 for the full list of operators.

##### Table 1.3. Operators

Operator

What It Does

x + y (Numeric)

x + y (String)

Concatenates x and y together

x - y

Subtracts y from x

x * y

Multiplies x and y together

x / y

Divides x by y

x % y

Modulus of x and y (i.e., the remainder when x is divided by y)

x++, ++ x

Adds one to x (same as x = x + 1)

x--, --x

Subtracts one from x (same as x = x - 1)

-x

Reverses the sign on x

Tips

• While both x++ and ++x add one to x, they are not identical; the former increments x after the assignment is complete, and the latter before. For example, if x is 5, y=x++ results in y set to 5 and x set to 6, while y=++x results in both x and y set to 6. The operator - (minus sign) works similarly.

• If you mix numeric and string values when adding two values together, the result is a string. For example, cat + 5 results in cat5.