JavaScript Variables


Declaring (Creating) JavaScript Variables
You can declare JavaScript variables with the var statement:
   var  x;            var  carname;
You can also assign values to the variables when you declare them:
   var  x = 5;        var  carname = "Volvo";

Local and Global JavaScript Variables Assigning Values to Undeclared JavaScript Variables
If you assign values to variables that have not yet been declared, the variables will automatically be declared. The following statements will declare the variables x and carname as global variables (if they do not already exist):
   x = 5;             carname = "Volvo";

JavaScript Arithmetic
As with algebra, you can do arithmetic operations with JavaScript variables:
   y = x-5;        z = y+5;

Demonstration
The following demonstration shows how the script of HTML and JavaScript is displayed on the Web.

     





      If you want to reach the island with the treasure,    
      you’ve got to swim with sharks (take a major risk) for a while.