Academic Tutorials



English | French | Portugese | German | Italian
Home Advertise Payments Recommended Websites Interview Questions FAQs
News Source Codes E-Books Downloads Jobs Web Hosting
Chats

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
CSS 1.0
CSS 2.0
HLML
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
DHTML Tutorial
HTML DOM Tutorial
WMLScript Tutorial
E4X Tutorial
Server Scripting
ASP Tutorial
PERL Tutorial
SQL Tutorial
ADO Tutorial
CVS
Python
Apple Script
PL/SQL Tutorial
SQL Server
PHP
.NET (dotnet)
Microsoft.Net
ASP.Net
.Net Mobile
C# : C Sharp
ADO.NET
VB.NET
VC++
Multimedia
SVG Tutorial
Flash Tutorial
Media Tutorial
SMIL Tutorial
Photoshop Tutorial
Gimp Tutorial
Matlab
Gnuplot Programming
GIF Animation Tutorial
Scientific Visualization Tutorial
Graphics
Web Building
Web Browsers
Web Hosting
W3C Tutorial
Web Building
Web Quality
Web Semantic
Web Careers
Weblogic Tutorial
SEO
Web Site Hosting
Domain Name
Java Tutorials
Java Tutorial
JSP Tutorial
Servlets Tutorial
Struts Tutorial
EJB Tutorial
JMS Tutorial
JMX Tutorial
Eclipse
J2ME
JBOSS
Programming Langauges
C Tutorial
C++ Tutorial
Visual Basic Tutorial
Data Structures Using C
Cobol
Assembly Language
Mainframe
Forth Programming
Lisp Programming
Pascal
Delphi
Fortran
OOPs
Data Warehousing
CGI Programming
Emacs Tutorial
Gnome
ILU
Soft Skills
Communication Skills
Time Management
Project Management
Team Work
Leadership Skills
Corporate Communication
Negotiation Skills
Database Tutorials
Oracle
MySQL
Operating System
BSD
Symbian
Unix
Internet
IP-Masquerading
IPC
MIDI
Software Testing
Testing
Firewalls
SAP Module
ERP
ABAP
Business Warehousing
SAP Basis
Material Management
Sales & Distribution
Human Resource
Netweaver
Customer Relationship Management
Production and Planning
Networking Programming
Corba Tutorial
Networking Tutorial
Microsoft Office
Microsoft Word
Microsoft Outlook
Microsoft PowerPoint
Microsoft Publisher
Microsoft Excel
Microsoft Front Page
Microsoft InfoPath
Microsoft Access
Accounting
Financial Accounting
Managerial Accounting
Network Sites


JavaScript Variables

Previoushome 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.

A D V E R T I S E M E N T
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>


View comments on this page.

Posted By jitendra kumar gupta on: Tuesday, September 7, 2010
variable
use variable




  JavaScript Tutorial eBooks

No eBooks on JavaScript could be found as of now.

 
 JavaScript Tutorial FAQs
More Links » »
 
 JavaScript Tutorial Interview Questions
More Links » »
 
 JavaScript Tutorial Articles
More Links » »
 
 JavaScript Tutorial News
More Links » »
 
 JavaScript Tutorial Jobs
More Links » »

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

Previoushome Next

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

HTML Quizzes
HTML Quiz
XHTML Quiz
CSS Quiz
TCP/IP Quiz
CSS 1.0 Quiz
CSS 2.0 Quiz
HLML Quiz
XML Quizzes
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 Quizzes
JavaScript Quiz
VBScript Quiz
DHTML Quiz
HTML DOM Quiz
WMLScript Quiz
E4X Quiz
Server Scripting Quizzes
ASP Quiz
PERL Quiz
SQL Quiz
ADO Quiz
CVS Quiz
Python Quiz
Apple Script Quiz
PL/SQL Quiz
SQL Server Quiz
PHP Quiz
.NET (dotnet) Quizzes
Microsoft.Net Quiz
ASP.Net Quiz
.Net Mobile Quiz
C# : C Sharp Quiz
ADO.NET Quiz
VB.NET Quiz
VC++ Quiz
Multimedia Quizzes
SVG Quiz
Flash Quiz
Media Quiz
SMIL Quiz
Photoshop Quiz
Gimp Quiz
Matlab Quiz
Gnuplot Programming Quiz
GIF Animation Quiz
Scientific Visualization Quiz
Graphics Quiz
Web Building Quizzes
Web Browsers Quiz
Web Hosting Quiz
W3C Quiz
Web Building Quiz
Web Quality Quiz
Web Semantic Quiz
Web Careers Quiz
Weblogic Quiz
SEO Quiz
Web Site Hosting Quiz
Domain Name Quiz
Java Quizzes
Java Quiz
JSP Quiz
Servlets Quiz
Struts Quiz
EJB Quiz
JMS Quiz
JMX Quiz
Eclipse Quiz
J2ME Quiz
JBOSS Quiz
Programming Langauges Quizzes
C Quiz
C++ Quiz
Visual Basic Quiz
Data Structures Using C Quiz
Cobol Quiz
Assembly Language Quiz
Mainframe Quiz
Forth Programming Quiz
Lisp Programming Quiz
Pascal Quiz
Delphi Quiz
Fortran Quiz
OOPs Quiz
Data Warehousing Quiz
CGI Programming Quiz
Emacs Quiz
Gnome Quiz
ILU Quiz
Soft Skills Quizzes
Communication Skills Quiz
Time Management Quiz
Project Management Quiz
Team Work Quiz
Leadership Skills Quiz
Corporate Communication Quiz
Negotiation Skills Quiz
Database Quizzes
Oracle Quiz
MySQL Quiz
Operating System Quizzes
BSD Quiz
Symbian Quiz
Unix Quiz
Internet Quiz
IP-Masquerading Quiz
IPC Quiz
MIDI Quiz
Software Testing Quizzes
Testing Quiz
Firewalls Quiz
SAP Module Quizzes
ERP Quiz
ABAP Quiz
Business Warehousing Quiz
SAP Basis Quiz
Material Management Quiz
Sales & Distribution Quiz
Human Resource Quiz
Netweaver Quiz
Customer Relationship Management Quiz
Production and Planning Quiz
Networking Programming Quizzes
Corba Quiz
Networking Quiz
Microsoft Office Quizzes
Microsoft Word Quiz
Microsoft Outlook Quiz
Microsoft PowerPoint Quiz
Microsoft Publisher Quiz
Microsoft Excel Quiz
Microsoft Front Page Quiz
Microsoft InfoPath Quiz
Microsoft Access Quiz
Accounting Quizzes
Financial Accounting Quiz
Managerial Accounting Quiz
Testimonials | Contact Us | Link to Us | Site Map
Copyright © 2008. Academic Tutorials.com. All rights reserved Privacy Policies | About Us
Our Portals : Academic Tutorials | Best eBooksworld | Beyond Stats | City Details | Interview Questions | Discussions World | Excellent Mobiles | Free Bangalore | Give Me The Code | Gog Logo | Indian Free Ads | Jobs Assist | New Interview Questions | One Stop FAQs | One Stop GATE | One Stop GRE | One Stop IAS | One Stop MBA | One Stop SAP | One Stop Testing | Webhosting in India | Dedicated Server in India | Sirf Dosti | Source Codes World | Tasty Food | Tech Archive | Testing Interview Questions | Tests World | The Galz | Top Masala | Vyom | Vyom eBooks | Vyom International | Vyom Links | Vyoms | Vyom World
Copyright © 2003-2017 Vyom Technosoft Pvt. Ltd., All Rights Reserved.