JavaScript Variables

## JS Basic

JS HOME JS Introduction JS How To JS Output JS Statements JS Comments JS Variables JS Data Types JS Objects JS Functions JS Operators JS Comparisons JS Conditions JS Switch JS Loop For JS Loop While JS Breaks JS Errors JS Validation

## JS Advanced

JS Object JS Number JS String JS Date JS Array JS Boolean JS Math JS RegExp

## JS HTML DOM

DOM Intro DOM Methods DOM Document DOM Elements DOM HTML DOM CSS DOM Events DOM Navigation DOM Nodes DOM Nodelist

## JS Browser BOM

JS Window JS Screen JS Location JS History JS Navigator JS PopupAlert JS Timing JS Cookies

## JS Libraries

JS Libraries JS jQuery JS Prototype

## JS Examples

JS Basic Examples JS Objects Examples JS DOM Examples JS HTML Examples JS Browser Examples JS Quiz JS Certificate JS Summary

## JS References

Javascript Objects HTML DOM Objects

# JavaScript Variables

« Previous
Next Chapter »

JavaScript variables are "containers" for storing information:

## Example

var x=5;
var y=6;
var z=x+y;
Try it yourself »

## Much Like Algebra

x=5
y=6
z=x+y

In algebra we use letters (like x) to hold values (like 5).

From the expression z=x+y above, we can calculate the value of z to be 11.

In JavaScript these letters are called variables.

| Think of variables as containers for storing data.

## JavaScript Variables

As with algebra, JavaScript variables can be used to hold values (x=5) or expressions (z=x+y).

Variable can have short names (like x and y) or more descriptive names (age, sum, totalvolume).

- Variable names must begin with a letter
- Variable names can also begin with $ and _ (but we will not use it)
- Variable names are case sensitive (y and Y are different variables)

| Both JavaScript statements and JavaScript variables are case-sensitive.

## JavaScript Data Types

JavaScript variables can also hold other types of data, like text values (person="John Doe").

In JavaScript a text like "John Doe" is called a string.

There are many types of JavaScript variables, but for now, just think of numbers and strings.

When you assign a text value to a variable, put double or single quotes around the value.

When you assign a numeric value to a variable, do not put quotes around the value. If you put quotes around a numeric value, it will be treated as text.

## Example

var pi=3.14;
var person="John Doe";
var answer='Yes I am!';
Try it yourself »

## Declaring (Creating) JavaScript Variables

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

You declare JavaScript variables with the var keyword:

var carname;

After the declaration, the variable is empty (it has no value).

To assign a value to the variable, use the equal sign:

carname="Volvo";

However, you can also assign a value to the variable when you declare it:

var carname="Volvo";

In the example below we create a variable called carname, assigns the value "Volvo" to it, and put the value inside the HTML paragraph with id="demo":

## Example

<p id="demo"></p>
var carname="Volvo";
document.getElementById("demo").innerHTML=carname;
Try it yourself »

| It's a good programming practice to declare all the variables you will need, in one place, at the beginning of your code.

## One Statement, Many Variables

You can declare many variables in one statement. Just start the statement with var and separate the variables by comma:

var lastname="Doe", age=30, job="carpenter";

Your declaration can also span multiple lines:

var lastname="Doe",
age=30,
job="carpenter";

## Value = undefined

In computer programs, variables are often declared without a value. The value can be something that has to be calculated, or something that will be provided later, like user input. Variable declared without a value will have the value undefined.

The variable carname will have the value undefined after the execution of the following statement:

var carname;

## Re-Declaring JavaScript Variables

If you re-declare a JavaScript variable, it will not lose its value:.

The value of the variable carname will still have the value "Volvo" after the execution of the following two statements:

var carname="Volvo";
var carname;

## JavaScript Arithmetic

As with algebra, you can do arithmetic with JavaScript variables, using operators like = and +:

## Example

y=5;
x=y+2;
Try it yourself »

You will learn more about JavaScript operators in a later chapter of this tutorial.

« Previous
Next Chapter »

## Your suggestion:

Your E-mail (optional):

Page address:

Description:

Close [X]

## Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

## WEB HOSTING

Best Web Hosting UK Reseller Hosting Wow! $5 SSD Hosting

## WEB BUILDING

Download XML Editor
FREE Website BUILDER FREE Website Creator

## WEB RESOURCES

Web Statistics Web Validation

## SHARE THIS PAGE