W3Schools
home HOME Tutorials | References | Examples | Forum | About
Search W3Schools :

JS Basic
JS HOME
JS Introduction
JS How To
JS Where To
JS Statements
JS Comments
JS Variables
JS Operators
JS Comparisons
JS If...Else
JS Switch
JS Popup Boxes
JS Functions
JS For Loop
JS While Loop
JS Break Loops
JS For...In
JS Events
JS Try...Catch
JS Throw
JS onerror
JS Special Text
JS Guidelines

JS Objects
JS Objects Intro
JS String
JS Date
JS Array
JS Boolean
JS Math
JS RegExp
JS HTML DOM

JS Advanced
JS Browser
JS Cookies
JS Validation
JS Animation
JS Image Maps
JS Timing
JS Create Object
JS Summary

Examples/Quiz
JS Examples
JS Object Examples
JS DOM Examples
JS Quiz

JS References
JS Objects
JS HTML DOM

Selected Reading
Web Statistics
Web Glossary
Web Hosting
Web Quality

W3Schools Tutorials
W3Schools Forum

Helping W3Schools

JavaScript Variables

previous next

Variables are "containers" for storing information:

x=5;  carname="Volvo";


Do You Remember Algebra From School?

Hopefully you remember algebra like this from school: x=5, y=6, z=x+y.

Do you remember that a letter (like x) could be used to hold a value (like 5), and that you could use the information above to calculate the value of z to be 11?

Sure you do!

These letters are called variables, and variables can be used to hold values (x=5) or expressions (z=x+y).


JavaScript Variables

As with algebra, JavaScript variables are used to hold values or expressions.

A variable can have a short name, like x, or a more descriptive name like carname.

Rules for JavaScript variable names:

  • Variable names are case sensitive (y and Y are two different variables)
  • Variable names must begin with a letter or the underscore character

NOTE:  Because JavaScript is case-sensitive, variable names are case-sensitive.


Example

A variable's value can change during the execution of a script. You can refer to a variable by its name to display or change its value.

This example will show you how


Declaring (Creating) JavaScript Variables

Creating variables in JavaScript is most often referred to as "declaring" variables.

You can declare JavaScript variables with the var statement:

var x;
var carname;

After the declaration shown above, the variables have no values, but you can assign values to the variables while you declare them:

var x=5;
var carname="Volvo";

Note: When you assign a text value to a variable, you use quotes around the value.


Assigning Values to JavaScript Variables

You assign values to JavaScript variables with assignment statements:

x=5;
carname="Volvo";

The variable name is on the left side of the = sign, and the value you want to assign to the variable is on the right.

After the execution of the statements above, the variable x will hold the value 5, and carname will hold the value Volvo.


Assigning Values to Undeclared JavaScript Variables

If you assign values to variables that have not yet been declared, the variables will automatically be declared.

These statements:

x=5;
carname="Volvo";

have the same effect as:

var x=5;
var carname="Volvo";


Redeclaring JavaScript Variables

If you redeclare a JavaScript variable, it will not lose its original value.

var x=5;
var x;

After the execution of the statements above, the variable x will still have the value of 5. The value of x is not reset (or cleared) when you redeclare it.


JavaScript Arithmetic

As with algebra, you can do arithmetic with JavaScript variables:

y=x-5;
z=y+5;

You will learn more about the operators that can be used between JavaScript variables in the next chapter of this tutorial.


previous next


FUNC Cheap Flights


diploma   

Get Your Diploma!

W3Schools' Online Certification Program is the perfect solution for busy professionals who need to balance work, family, and career building.

The HTML Certificate is for developers who want to document their knowledge of HTML, XHTML, and CSS.

The JavaScript Certificate is for developers who want to document their knowledge of JavaScript and the HTML DOM.

The ASP Certificate is for developers who want to document their knowledge of ASP, SQL, and ADO.



Jump to: Top of Page or HOME or Printer Friendly Printer friendly page

W3Schools provides material for training only. We do not warrant the correctness of its contents. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.

Copyright 1999-2008 by Refsnes Data. All Rights Reserved.

Validate Validate W3C-WAI level A conformance icon W3Schools was converted to XHTML in December 1999
Web charting
Web based charting
for ASP.NET
WEB HOSTING
$15 Domain Name
Registration
Save $20 / year!
Buy UK Domain Names
Register Domain Names
Cheap Domain Names
Cheap Web Hosting
Best Web Hosting
PHP MySQL Hosting
Top 10 Web Hosting
Web Hosting Company
UK Reseller Hosting
Web Hosting
WEB BUILDING
Website Templates
Flash Templates
Website Builder
Internet Business Opportunity
Custom Programming
FREE Trial or Demo
Web Content Manager
Forms,Web Alerts,RSS
Download XML editor
HTML Tutorial DVD
EDUCATION
US Web Design Schools
HTML Exam
TRAVEL
FUNC Cheap Flights
YELLOW PAGES
www.nettkatalogen.no
www.gulex.dk
www.gulex.se
www.gelbex.de
www.teloos.fi
www.teloos.fr
www.teloos.co.uk
www.teloos.at
www.teloos.de