|
Having data and expressions is fine, but
it's not of much use if you cannot store it some where. Variables
enable you to define places to store this data. Think of a
variable as a storage container. It always contains something,
but what it contains can change over time.
| Defining and Naming Variables |
It's extremely simple to define a variable
in JavaScript. Think of a name for the variable that you want
to define and then put var before it. You can either initialise
a value or create a variable without an initial value. If
you create a variable using second case then you should be
careful not to access the variable before it has had a value
placed in it.
Rules to follow while creating a variables:
|
|
The variables name cannot
be reserved word. |
|
|
The first character in
the name must be an alphabetic or Underscores(_) |
|
|
The first character in
the variable name can be alphabetic, letters, number or underscores. |
| Valid variables names: |
Invalid variables names: |
| Varx_location; |
Var99baloons: |
// Violates rule 2 |
| Var_location; |
Var eggsSbacon: |
// Violates rule 3 |
| Var ven 32; |
Var $ ven; |
// Violates rule 4 |
| Var question_22; |
Var package; |
// Violates rule 1 |
|
| Changing the
Value of a Variable |
To give a variable a new value after it
is created, the assignment operator (=) is used. The variable
name is listed on the left-hand side of the statement, and
an expression containing the value it has to be assigned is
listed on the right-hand side of the statement.
Example :
myvalue = 10;
myvalue = true;
myvalue = "Football";
Copyrights : Layout Galaxy All Rights Reserved
No part of this tutorial may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, electrostatic, magnetic tape, mechanical or otherwise, without prior permission in writing from Layout Galaxy.
|
|