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};
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};
Like School Algebra
Remember algebra from school?x=5
y=6
z=x+y
Do you remember that letters (like x) can be used to hold a value (like 5), and that you can 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).
Think of variables as names or labels given to values. |
JavaScript Variables
As with algebra, JavaScript variables are used to hold values or expressions.Variable can have a short names, like x and y, or more descriptive names, like age, sum, or, totalvolume.
JavaScript variables can also be used to hold text values, like: name="John Doe".
Here are the 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
Both 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;
To assign a value to the variable, use the equal sign:
carname="Volvo";
var carname="Volvo";
Example
<p id="demo"></p>
var carname="Volvo";
document.getElementById("demo").innerHTML=carname;
var carname="Volvo";
document.getElementById("demo").innerHTML=carname;
It's a good programming practice to declare all the variables you will need, in one place, at the beginning of your code. |
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.
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 name="Doe", age=30, job="carpenter";
var name="Doe",
age=30,
job="carpenter";
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;
var carname;
JavaScript Arithmetic
As with algebra, you can do arithmetic with JavaScript variables, using operators like = and +:Example
y=5;
x=y+2;
x=y+2;