Showing posts with label JavaScript Variables. Show all posts
Showing posts with label JavaScript Variables. Show all posts

Monday, August 27, 2012

JavaScript Variables


Variables are "containers" for storing information:

JavaScript Data Types

var answer1="He is called 'Johnny'";
var answer2='He is called "Johnny"';var pi=3.14;

var x=123;
var y=123e5;
var z=123e-5;
var cars=new Array("Saab","Volvo","BMW");
var person={firstname:"John", lastname:"Doe", id:5566};

Try it yourself »


Do You Remember Algebra From School?

Do you remember algebra 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?

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

lampThink of variables as names or labels given to values.


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, the $ character, or the underscore character
lampBoth JavaScript statements and JavaScript variables are case-sensitive.


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 shown above, the variable is empty (it has no value yet).

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";

After the execution of the statement above, the carname will hold the value Volvo.

Example

var carname="Volvo";
document.getElementById("myP").innerHTML=carname;

Try it yourself »


JavaScript Data Types

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

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

Re-Declaring JavaScript Values

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

Local JavaScript Variables

A variable declared within a JavaScript function becomes LOCAL and can only be accessed within that function. (the variable has local scope).

You can have local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared.

Local variables are deleted as soon as the function is completed.

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

Global JavaScript Variables

Variables declared outside a function, become GLOBAL, and all scripts and functions on the web page can access it.

Global variables are deleted when you close the page.

Assigning Values to Undeclared JavaScript Variables

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

This statement:

carname="Volvo";

will declare the variable carname as a global variable (if it does not already exist).

JavaScript Arithmetic

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

Example

y=5;
x=y+2;

Try it yourself »
You will learn more about the operators that can be used in a later chapter of this tutorial.