CHAPTER 2

image

Variables

Variables are containers used for storing data, such as numbers or strings, so that they can be used multiple times in a script.

Declaring Variables

To create a variable the var keyword is used followed by a name, called the identifier. A common naming convention for variables is to have each word initially capitalized, except for the first one.

var myVar;

A value can be assigned to a variable by using the equals sign, which is called the assignment operator (=). This is called assigning or initializing the variable.

myVar = 10;

The declaration and assignment can be combined into a single statement. When a variable is assigned ...

Get JavaScript Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.