Defining Variables

You use variables in JavaScript to temporarily store and access data from your JavaScript files. Variables can point to simple data types such as numbers or strings, or they can point to more complex data types such as objects.

To define a variable in JavaScript, you use the var keyword and then give the variable a name, as in this example:

var myData;

You can also assign a value to the variable in the same line. For example, the following line of code creates a variable myString and assigns it the value "Some Text":

var myString = "Some Text";

This single line does the same thing as the following two lines:

var myString;myString = "Some Text";

After you have declared a variable, you can use its name to assign a value to the ...

Get Node.js, MongoDB, and AngularJS Web Development 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.