Academic Tutorials



English | French | Portugese | German | Italian
Google

Home Source Codes E-Books Downloads Contact Us About Us

JavaScript Tutorial
JavaScript Introduction
JavaScript How To
JavaScript Where To
JavaScript Variables
JavaScript If...Else Statement
JavaScript Switch Statement
JavaScript Operator
JavaScript Popup-boxes
JavaScript Function
JavaScript For-Loop
JavaScript While-loops
JavaScript Break Loops
JavaScript For..In Statement
JavaScript Event
JavaScript Try-Catch Statement
JavaScript Throw Statement
JavaScript Onerror
JavaScript Spacial Characters
JavaScript Guideline
JavaScript String
JavaScript Date Object
JavaScript Array
JavaScript Boolean Object
JavaScript Math Object
JavaScript HTML DOM Object
JavaScript Browser Detection
JavaScript Cookies
JavaScript Validation
JavaScript Animation
JavaScript Image Maps
JavaScript Timing Event
JavaScript Create Object
JavaScript Summary

HTML Tutorials
HTML Tutorial
XHTML Tutorial
CSS Tutorial
TCP/IP Tutorial
XML Tutorials
XML Tutorial
XSL Tutorial
XSLT Tutorial
DTD Tutorial
Schema Tutorial
XForms Tutorial
XSL-FO Tutorial
XML DOM Tutorial
XLink Tutorial
XQuery Tutorial
XPath Tutorial
XPointer Tutorial
RDF Tutorial
SOAP Tutorial
WSDL Tutorial
RSS Tutorial
WAP Tutorial
Web Services Tutorial
Browser Scripting
JavaScript Tutorial
VBScript Tutorial
AJAX Tutorial
DHTML Tutorial
HTML DOM Tutorial
WMLScript Tutorial
E4X Tutorial
Server Scripting
ASP Tutorial
PHP Tutorial
PERL Tutorial
SQL Tutorial
ADO Tutorial
.NET (dotnet)
Microsoft.Net
XML Web Services
ASP.Net
.Net Mobile
C# : C Sharp
ADO.NET
VB.NET
Multimedia
SVG Tutorial
Flash Tutorial
Media Tutorial
SMIL Tutorial
Web Building
Web Browsers
Web Hosting
W3C Tutorial
Web Building
Web Quality
Web Semantic
Web Careers
Java Tutorials
Java Tutorial
JSP Tutorial
Servlets Tutorial
Struts Tutorial
EJB Tutorial
JMS Tutorial
JMX Tutorial
Programming Langauges
C Tutorial
C++ Tutorial
Visual Basic Tutorial
Data Structures Using C
Soft Skills
Communication Skills
Time Management
Project Management
Team Work
Leadership Skills
Corporate Communication
Negotiation Skills


JavaScript Variables
Previous Next

A variable is a "container" for data or information you want to store.

A variable is an area inside the memory set aside to store certain data, or you can call it a "container" that stores your data. In a matter of speaking you can put data into the container, retrieve data from it or change the contents of this "container".You refer to the variable by its name (variable identifier). Remember that in Java Script variable names are case sensitive thus "age" is not the same as "AGE" or "Age". All variable names must begin with letter or the underscore (_) character.

Java Script is an untyped language.This means that Java Script variables can hold data of any valid type.It takes its type from the data type of what it is holding.You cannot declare a type for variables in Java Script.There is no facility for saying this variable must be a number,or this one must be a string.




Declaring the variables

Variables name can not contain spaces, or begin with any number, and all punctuation symbols except for the underscore (_) are restricted. Your variable names should be descriptive of the value they represent if for no other reason than script readability. Be careful to not use reserved Java Script keywords for your variable names. A common convention to help avoid using reserved words is using two (or more) word combinations. The two words can either be separated by an underscore (my_variable) or by capitalizing the second word (myVariable). While I prefer the latter, the convention you choose is up to you.




Variable Scope

Variables scope has to do with where a variables can legally be used, and is determined by where it was originally declared or initialized. A variables declared or initialized outside a function body has a global scope, making it accessible to all others statement within the same document. A variables declared or initialized within a function body has a local scope, making it accessible only to statements within the same function body.




Variable Scope - Special Case

If variables are initialized inside a function body but the var keyword is left off, the variables have a global scope and this is only accessible after the function containing it is invoked. It is safer to always use the var keyword, begining or pro.

You declare variables in JavaScript with the var keyword. You can declare multiple variables at once. You can also declare a variable and assign it a value at the same time. Until you assign a value to a variable it is undefined.

If you try to declare a variable that already maych or existsin same program, Java Script will treat it as a simple assignment statement and assign any new value in the declaration statement to the variable.If the duplicate declaration has no assignment, then nothing happens. If you try to assign a value to a non-existent or non match variable, Java Script will create the variable for you.

Here is some code creating and assigning values to a couple of variables:

<script type="text/javascript">
//Commented lines starting with the double
//forward slash will be ignored by JavaScript

//First we will declare a few variables
//and assign values to them

myText = "Good day!";
myNum = 5;
//Note that myText is a string and myNum is numeric.

//Next we will display these to the user.
document.write(myText);

//To concatenate strings in JavaScript, use the '+' operator
document.write("My favourite number is "+ myNum);
</script>




Variable Naming Conventions

JavaScript variables must start with a letter or underscore "_".

JavaScipt is case sensitive.

Case sensitivity (small and big letter in the code is not same)is the thing that causes many problems and take hours of finding mistakes. Have a look at the following code:

<script type="text/javascript">

var myVar = "WebCheatSheet";
var myvar = "Vyom Acedmic JavaScript Tutorial";

//outputs "WebCheatSheet - Vyom Acedmic JavaScript Tutorial"
document.write(myVar + " - " + myvar);
</script>



Variable Scope and Lifetime

All variable has scope. The scope is the region of the program for which the variable is declared. Variables are either Global,or Local.Local variables are available only within the section of code in which they were defined.Changes to Local variables are not reflected 'outside' their area of definition.When you exit this area, the variable is autometic destroyed. Global variables are available to other Java Script code. Generally, variables declared as "var" are Global variables. Variables declared inside a function as "var" are local to the function. Variables defined inside a function without the "var" are Global variables. The lifetime of Global variables starts when they are declared, and ends when the page is closed.

<script type="text/javascript">
var altitude = 5;            //GLOBAL

function square( ) {
base = 17;             //GLOBAL
sqr = 0.5*(base + altitude);
return sqr;
}

function perimeter() {
var side = 7.5;             //LOCAL
prm = 2*side + base;
return prm;

}
</script>



Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • connotea
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Netvouz
  • RawSugar
  • Reddit
  • scuttle
  • Shadows
  • Simpy
  • Smarking
  • Spurl
  • TailRank
  • Wists
  • YahooMyWeb

Previous Next

Keywords:javascript global variables, javascript variables operators, javascript variables characters, variable names


HTML Quizes
HTML Quiz
XHTML Quiz
CSS Quiz
TCP/IP Quiz
XML Quizes
XML Quiz
XSL Quiz
XSLT Quiz
DTD Quiz
Schema Quiz
XForms Quiz
XSL-FO Quiz
XML DOM Quiz
XLink Quiz
XQuery Quiz
XPath Quiz
XPointer Quiz
RDF Quiz
SOAP Quiz
WSDL Quiz
RSS Quiz
WAP Quiz
Web Services Quiz
Browser Scripting Quizes
JavaScript Quiz
VBScript Quiz
AJAX Quiz
DHTML Quiz
HTML DOM Quiz
WMLScript Quiz
E4X Quiz
Server Scripting Quizes
ASP Quiz
PHP Quiz
PERL Quiz
SQL Quiz
ADO Quiz
.NET (dotnet) Quizes
Microsoft.Net Quiz
XML Web Services Quiz
ASP.Net Quiz
.Net Mobile Quiz
C# : C Sharp Quiz
ADO.NET Quiz
VB.NET Quiz
Multimedia Quizes
SVG Quiz
Flash Quiz
Media Quiz
SMIL Quiz
Web Building  Quizes
Web Browsers Quiz
Web Hosting Quiz
W3C Quiz
Web Building Quiz
Web Quality Quiz
Web Semantic Quiz
Web Careers Quiz
Java Quizes
Java Quiz
JSP Quiz
Servlets Quiz
Struts Quiz
EJB Quiz
JMS Quiz
JMX Quiz
Programming Langauges Quizes
C Quiz
C++ Quiz
Visual Basic Quiz
Data Structures Using C Quiz
Soft Skills Quizes
Communication Skills Quiz
Time Management Quiz
Project Management Quiz
Team Work Quiz
Leadership Skills Quiz
Corporate Communication Quiz
Negotiation Skills Quiz

Privacy Policy
Copyright © 2003-2008 Vyom Technosoft Pvt. Ltd., All Rights Reserved.