www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons JavaScript Variables Bookmark and Share
Tutorial Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
Lesson 41
Lesson 42
Lesson 43
Lesson 44
Lesson 45
Lesson 46
Lesson 47
Lesson 48
Lesson 49

A variable is a named space in computer memory that holds some value. We use variables in our programs to store information or data that we need and use in the course of the execution of our programs.

As the name suggests the value a variable contains can vary and is changeable during the execution of a program.

We can put data or values in our variables, retrieve them and / or change their contents. As JavaScript is a loosely typed language, variables in JavaScript can hold values of any valid type and we do not need to specify a type for them. The data type of a variable is determined automatically, depending on the type of the value we assign to it.

Variable Declaration:
JavaScript is a case sensitive language and so are the variable names. Var1 is not the same as var1 or VAR1. A valid variable name must start with a letter or the underscore (_) letter. Variables name can not contain spaces. To improve the readibility of the code we should coose meanigful names for our variables. JavaScript keywords cannot be used as variable names.

We declare variables in JavaScript with the var keyword. The use of the var keyword, however, is optional.

var myVar;
myVar = 10;

We can also declare multiple variables on a single line:

var myVar1, myVar2, myVar3;
myVar1 = 10;
myVar2 = 20;
myVar3 = 30;

We can declare a variable and assign it a value in the same statement:

var myVar = 10;

Until or unless we assign a value to a variable its value is undefined.

The following code example illustrates the usage of variables in JavaScript:

<script type="text/JavaScript">
myText = "Good day!";
myNum = 5;
document.write("My favourite number is "+ myNum);

Next >>> Lesson No. 5: JavaScript Variables Scope

Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.