Variables

Variables are containers that stores values of various data type. A variable has a name and a value.


Rules for naming a variable:



Reserved and Keywords
abstract boolean break byte case catch
char class const continue debugger default
delete do double else enum export
extends false final finally float for
function goto if implements import in
instanceof int interface long native new
null package private protected public return
short static super switch synchronized this
throw throws transient true try typeof
var void volatile while with



Primitive Data Type



Function and Object Data Type




Declaring a Variable

A variable starts with the keyword var followed by the name of the variable and ends with a semi-colon. It is helpful to declare variables with meaningful names.

Syntax

var nameOfVariable;

Examples:

var a;
var b;
var x;
var y;
var myAge;
var firstName;
var postCode;
var seatEmpty;
var room_7;
var red_Door;
var car_Price
var last_Year;


You can declare more then one variable in a single line followed by a comma after each name and end it with a semi-colon.

var myAge,firstName,red_Door,seatEmpty,car_Price,x,y;



Initialising a Variable

Once a variable has been declared you have to initialise it in other words assign a value. The equal sign is used to assign the value.

a=10;
b=2;
x=5;
y=2.5;
var myAge=34;
var firstName="Jane"
var postCode="EC2";
var seatEmpty=false;
room_7=7;
var red_Door=true;
var car_Price=2543.50
var last_Year=false

The values of a variable can change from a number to a string. If you are going to declare a variable that holds numbers then stick to numbers. Otherwise, your codes could get messy.

Let us declare a variable age as a number and then change the variable age to a string.

<script>
var age=44; (declared as a number)
document.write("my age is " + age + "<br>");

age="forty four"; (changed to a string)
document.write( "my age is " + age);
</script>

Displays




Constant Value

The keyword const is used instead of var, so the variable value remains constantly the same through out the program.

Let's write a program that finds the area of a circle. We want the value of pi to be constant, so the keyword const will be used instead of var.

<script>
const pi= 3.14;
var radius=4;
var area;
document.write("The area of a circle is " )
document.write(area=pi*(radius*radius));
</script>

Displays




Converting Data Type

There are functions, which you can use to convert the value of a string to a number or vice versa.

parseInt()

The parseInt() functions lets you convert a string to an integer. The value of the string conversion must be inside the parseInt parentheses with quotation marks.


<script>
var num1="8";
document.write("String:"+ num1 + 20);
document.write("Number:"+(parseInt("8")+20));
</script>

Displays


parseFloat()

The parseFloat() converts a string to a decimal number.

<script>
var num1="10.5";
document.write("String:"+ num1 + 20);
document.write("Number:"+(parseFloat("10.5")+20));
</script>

Displays



Variable Scope

There are two variable scopes local and global.

Global

If you assign a variable outside a function it is called a global variable. It can be accessed by any of the functions on that page.

Local

If you assign a variable inside a function it is called a local variable. It can only be accessed by that particular function.

However, if you declare a variable inside a function without the keyword var then it becomes a global variable.

In the example below we have declared two global variables a and b and have assigned the values 10 and 12. Inside the function we have declared a variable called answer, which has not been assigned a value.

However, when the function addition() is called the the value of answer will be the sum of a and b.


<script>

var a =10;
var b =12;
function addition()
{
var answer;
document.write("Answer=" + (answer=a+b));
}
<script>

Displays





Now do the Exercises!