How Do You Declare A Variable In Javascript?
• Declaring Javascript variables : In Javascript variables are created using “var” keyword. When using a variable for the first time it is not mandatory to use “var” before the variable name, but it is a good practice to make it clear when a variable is id used for the first time in the program. Variables are declared as follows: var x; var myVar; • Rules for the variable names: • Variable names are case sensitive • Variable names must begin with a letter or the underscore character • Variable names should not be Javasript reserved names like boolean, long, null, for, if, else …etc • Scope of the Javascript variable: The variable declared can have either • Local Scope: Variables declared within a function are scoped to that function. There can be local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared and they are destroyed on exiting from the function • Global Scope: Variables declared globally