JavaScript Variables
JavaScript Variables:
JavaScript Variables are containers for storing data values. Variables are declared with "var" keyword as follows.
var x=5;
var y=6;
var z = x+y;
Rules for constructing names for variables are:
1) Names can contain letters, digits, underscores and dollar signs.
2) Names must be begin with a letter.
3) Names can also begin with $ and _
4) Names are case sensitive.
5) Reserved words (like javascript keywords) can not be used as names.
JavaScript is untyped language. This means that a JavaScript variable can hold a value of any data type.
Daclaring (Creating) JavaScript Variables:
Creating a variable in JavaScript is called "declaring" a variable.
var bikeName;
After the declaration, the variable has no value. (Technically it has the value of undefined)
carName = "Hero Honda";
We can declare many variables in one statement;
var person="Pavan Kumar", bikeName="Hero Honda", price=50000;
A declaration can span multiple lines:
var person="Pavan Kumar",
bikeName="Heron Honda",
price=50000;
If we re-declare a JavaScript variable, it will not lose its value.
The variable bikeName will still have the value "Hero Honda" after the execution of these statements:
var bikeName="Hero Honda";
var bikeName;
JavaScript Variables are containers for storing data values. Variables are declared with "var" keyword as follows.
var x=5;
var y=6;
var z = x+y;
Rules for constructing names for variables are:
1) Names can contain letters, digits, underscores and dollar signs.
2) Names must be begin with a letter.
3) Names can also begin with $ and _
4) Names are case sensitive.
5) Reserved words (like javascript keywords) can not be used as names.
JavaScript is untyped language. This means that a JavaScript variable can hold a value of any data type.
Daclaring (Creating) JavaScript Variables:
Creating a variable in JavaScript is called "declaring" a variable.
var bikeName;
After the declaration, the variable has no value. (Technically it has the value of undefined)
carName = "Hero Honda";
We can declare many variables in one statement;
var person="Pavan Kumar", bikeName="Hero Honda", price=50000;
A declaration can span multiple lines:
var person="Pavan Kumar",
bikeName="Heron Honda",
price=50000;
If we re-declare a JavaScript variable, it will not lose its value.
The variable bikeName will still have the value "Hero Honda" after the execution of these statements:
var bikeName="Hero Honda";
var bikeName;
As with algebra, you can do arithmetic with JavaScript variables, using operators like = and +:
Example:
var x="5"+2+3;
Output:
523
523
Global Variables: A global variable has global scope which means it can be
defined anywhere in 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.
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment