Dot Net For All

Learning JavaScript – function explained

In my previous  post I have discussed about the data types in Javascript in which I have discussed about the Javascript function in brief. In this article I want to extend the discussion about the functions with code examples.

Passing Parameter to functions in Javascript

Suppose if I define a function with certain number of parameters and if I don’t provide value for all the parameter in that case the the last parameters for which the value has not been passed will remain as ‘undefined’ as shown in the below code.

function foo(a, b, c)
{ 
 alert(a); //1 
 alert(b); //undefined 
 alert(c); //undefined 
}; 
foo(1);

Javascript is well aware of the fact that while passing the parameters to the functions it is not necessary that it will receive all of them and while coding we should be aware of this fact.

Overriding a javascript function

Does javascript really supports overriding. Not really !!. Suppose if I have two functions with same name, the function which is defined later in the code will be called as shown in the below code snippet

	function foo(a, b, c){
		alert(a); 
		alert(b); 
		alert(c); 
	};
	
	function foo(a){
		alert(a);
	}
	
	foo(1);

In the above code snippet the function with single parameter will be always called. The first function just vanishes.

arguments object

As discussed in the first part of this article, when calling a javascript function we can pass any number of parameters. But we have an argument object present only in the scope of the functions by the help of which we can get the count of arguments passed as well as the arguments itself as shown in the code below

	function foo(a, b, c){
		alert(arguments.length);
		alert(arguments[0]);
	};	
	
	foo(1); //1
	foo(1,2,3,4,5); //5

While using argument object the declared parameter really does not matter as we can simple access the arguments using argument object as shown below.

	function foo(){		
		alert(arguments[0]);
	};	
	
	foo(1); //1
	foo(1,2,3,4,5); //1

argument object is just the array, means that all the functions of array object are applicable to argument object.

Returning a value from function

Javascript functions explicitly does not have a return type unlike C#. They just return values. If the function is not returning anything, still the function can be assigned to variable but the type of this variable will be undefined.

function foo(){	
		
	};
	var myvar = foo()// typeof undefined

Even if we specify only return as statement in that case also it will be undefined. But if we return some value in that case the type will be returned value’s type

		function foo(){		
			return "string";
		};	
		
		var myvar = foo();
		alert(typeof myvar); //string

Function as Objects

Functions just behave as any other object in javascript which will be clear in the below code snippet. Using the function names length property we get the number of parameters of the function, name of the function and the body of the function.

function foo(){	
		};	
		
		alert(foo.length); // 0
		alert(foo.name); // foo
		alert(foo.toString()); // returns body of the function

In this article I have discussed about the functions in javascript.

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview