1 / 8

JavaScript Variables

JavaScript Variables. Definition. A variable is a "container" for information you want to store. A variable's value can change during the script. Rules for JavaScript Variables. Can contain any letter of the alphabet, digits 0-9, and the underscore character. No spaces

aolani
Télécharger la présentation

JavaScript Variables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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: • varstrname = "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 • <script type="text/javascript"> • <!-- • varmyVar = "global"; // Declare a global variable • function checkscope( ) { • varmyVar = "local"; // Declare a local variable • document.write(myVar); • } • //--> • </script>

  8. The End …Thank you…

More Related