Variables

Computer programs use variables to store information.

Rules for JavaScript Variables

  • Variable names are case sensitive e.g. y and Y are different
  • `Variable names must begin with a letter or underscore e.g. year, _month

Declaring JavaScript Variables

In JavaScript variables can be declared in the following ways:

  • var x;
  • var x=5;
  • var firstNumber,secondNumber,number1,number2,sum;

Javascript is Dynamically Typed

JavaScript is dynamically typed. This means you don't have to declare the type of variable in advanced. The value you assign to the variable determines the type.

var  myNumber = 42;    // is a Number
var  myString = "bar"; // is a string
var  myBoolean = true;  // is a boolean

Performing Maths

You can apply mathematic operations to numbers using some basic operators like:

 var x = 5;  
 var y = 20;  
 var result = x + y; // result will equal 25
 var result = x * y; // result will equal 100 
 var result = y/x;  //  result will equal 5