Presentation is loading. Please wait.

Presentation is loading. Please wait.

JavaScript Variables. Definition A variable is a "container" for information you want to store. A variable's value can change during the script.

Similar presentations


Presentation on theme: "JavaScript Variables. Definition A variable is a "container" for information you want to store. A variable's value can change during the script."— Presentation transcript:

1 JavaScript Variables

2 Definition A variable is a "container" for information you want to store. A variable's value can change during the script.

3 Rules for JavaScript Variables Can contain any letter of the alphabet, digits 0-9, and the underscore character. No spaces No punctuation characters (eg comma, full stop, etc) The first character of a variable name cannot be a digit. JavaScript variables' names are case-sensitive. For example firstName and FirstName are two different variables.

4 Declare a Variable you can create a variable with the var statement: var strname = some value You can also create a variable without the var statement: strname = some value

5 Assign a Value to a Variable you can assign a value to a variable like this: var strname = "Hege" Or like this: strname = "Hege" The variable name is on the left side of the expression and the value you want to assign to the variable is on the right. Now the variable "strname" has the value "Hege".

6 JavaScript Variable Scope: The scope of a variable is the region of your program in which it is defined. JavaScript variable will have only two scopes. Global Variables: A global variable has global scope which means it is defined everywhere in your JavaScript code. Local Variables: A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.

7 Example <!-- var myVar = "global"; // Declare a global variable function checkscope( ) { var myVar = "local"; // Declare a local variable document.write(myVar); } //-->

8 The End …Thank you…


Download ppt "JavaScript Variables. Definition A variable is a "container" for information you want to store. A variable's value can change during the script."

Similar presentations


Ads by Google