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 variable and access the value of the variable. For example, the following code stores a string into the myString variable and then uses it when assigning the value to the newString variable:

var myString = "Some Text";
var newString = myString + " Some More Text";

You should give variables descriptive names so you know later what data they store and can more easily use them in your programs. A variable name must begin with a letter, $, or _, and it cannot contain spaces. In addition, variable names are case sensitive, so myString is different from MyString.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.47.51