Variable Declarations

Variables in JavaScript are untyped. This means that a variable can hold any value i.e. a character, string or number. This can be seen in the following example where the variable 'x' is set to a number of different values.

   1 var x = 10;
   2 print(x);
   3 var x = "test";
   4 print(x);
   5 var x = true;
   6 print (x);

You can test this using the Seed Console.

An important note has to be made here. When declaring variables, it is possible to omit the 'var' keyword, as seen in this example:

   1 x = 10;
   2 my_string = "test";
   3 can_do = false;

However, while it is technically possible to do this, it is not good practise because of the way variables are scoped in JavaScript.

When the programs you write becomes longer, you will need to split them up into functions to make them easier to understand. In this context, variables can become global where they can be used in any function or anywhere in the program. This is different to local variables which can only be used in the function they were created.

Whenever you create a variable prefixing it with var, you are creating a new instance of it. You can create a variable with the same name both inside and outside a function and while they may have the same name, they are in fact different variables. The one outside the function is the global variables where the one inside the function is local. The following example JS file shows x in the context as a global and local variable:

   1 #!/usr/bin/env seed
   2 
   3 // global declaration of x
   4 var x = 10;
   5 
   6 function test() 
   7 {
   8   // local declaration of x
   9   var x = 90;
  10   print ("this is contents of x inside function test:" + x);
  11 }
  12 
  13 print ("this is x before function:" + x );
  14 test();
  15 print ("this is x after function:" + x );

Details of how to run JavaScript files, click here.

The output of program is as follows:

this is x before function:10
this is contents of x inside function test:90
this is x after function:10

If you did not prefix var when declaring x inside function test, it would be interpreted as the global variable and not as a local one. It's contents would be changed inside the function and would be outside. This is shown in the modified program:

   1 #!/usr/bin/env seed
   2 
   3 // global declaration of x
   4 var x = 10;
   5 
   6 function test() 
   7 {
   8   // Setting global variable to value of 90 
   9   x = 90;
  10   print ("this is contents of x inside function test:" + x);
  11 }
  12 
  13 print ("this is x before function:" + x );
  14 test();
  15 print ("this is x after function:" + x );

The output being:

this is x before function:10
this is contents of x inside function test:90
this is x after function:90

At this moment you may be saying so what as there may be instances when you may want to change contents of variables inside functions and be the same outside it? The answer is you can return these variables in a more structured way which will be described in part about functions. As well as writing more structured code, it is also better to keep the habit of using var because if you are writing a large program, it may not be working properly because a function changes the contents of a global variable where is not supposed to because the local variable was not declared with var prefixed!! While this may not seem important now when just learning JavaScript, one day you will writing largish programs and it will be!

Another way of declaring a variable is by making it a constant variable. This means that you declare a variable with an initial value and it can't be changed after that. Again, this is useful in big programs where for example, rather than entering numbers throughout the program, you can enter it as a variable whose name would sense to you when coming back to the code after you had wrote it. As they are constant, there is no fear of accidentally writing a line of code that changed the value and affecting it later where it is required.

To declare a variable, enter the following:

   1 const variable = value;

i.e.

   1 const maximum_number_jobs = 10;

To show how a constant variables can't be changed, enter the following in the seed console:

   1 const last_xmas_date = "25/12/2009";
   2 
   3 print (last_xmas_date);
   4 
   5 last_xmas_date = "10/09/2001";
   6 
   7 print (last_xmas_date);

Projects/Seed/Tutorial/basic/var_dec (last edited 2013-11-22 19:19:58 by WilliamJonMcCann)