Skip navigation

Monthly Archives: February 2009


 

function add(x, y) {
    var total = x + y;
    return total;
}
  • The return statement is optional. 
  • If no return statement is used (or an empty return with no value), JavaScript returns undefined.
  • The named parameters turn out to be more like guidelines than anything else. You can call a function without passing the parameters it expects, in which case they will be set to undefined.
  • alert(add());

We can just call without any parameters

add();

in which case it will return NaN because you can’t add undefined values.

Even you can call 

add(2,3,4,5);

in which case it will return 5, ignoring 4 and 5.

OOPs guys will call it as FUNCTION OVERLOADING.


Function as Class definition

function Shape(width, height) {
  this.width = width;
  this.height = height;
}

Now you can create object using new keyword like

var objShape = new Shape(5,4);
alert(objShape.width); 

Function as class method

var Shape = {width:4,height:5}

Shape.show = showWidth;

function showWidth(){
  alert(this.width);
}

Shape.show();


function IllRetun() {
return
true
}
if(IllRetun()) alert(IllRetun()) else alert(‘bad return’)

You will be alerted with bad return; Lesson: Don’t forget semicolon;

Follow

Get every new post delivered to your Inbox.

Join 150 other followers