Sunday, 9 January 2011

Javascript: Functions and Variable Declarations

This is Part II of the Javascript Series.


Javascript Functions


We can write functions in Javascript. The coding for functions is this:


function testFunction()
{


}


I prefer to group the {} in one line. It is neater and it is not as confusing. You can see it at the same line. It is useful if there are a lot of functions written.


Functions is used to perform actions that user requires. 


Examples of actions are as below: 

  1. Perform screen navigations from one page to another or opening a popup
  2. Save information to the system
  3. Able to execute server script such as VBScript
  4. Hide and display certain fields 
  5. Perform validation checking
  6. Perform simple calculations
  7. Control how a page is being displayed on the screen


Variable Declarations


Here are how we declare variables in Javascript. There are several ways to declare methods. We can declare variables in one line or multiple lines as shown below.


var a;
var b;


var a,b;


Assigning Variables
After declaring variables, we can assign values to the variables.


var a = "First variable";
var b = "Second variable";


var a = "First variable";var b = "Second variable";


We can also assign value using this method:


var a,b;
a = "First variable";
b = "Second variable";


This concludes the second part on Javascript. We will be looking at "For" loops and "If' statements. It is slightly similar to the style used in Java and C#. 


For your information, Javascript <> Java. 


Cheers,


Rogerkoo

No comments:

Post a Comment