Take a look at Javascript function

Javascript’s function is like a block of code which can be called with a simple statement like this,

hello();

Function can have return type and function can either receive a parameter or without parameter. You start a function with the function keyword like so follows by the name of the function and then the open { and close }.

function hello() {
   //statement goes here.
}

Below is a complete Javascript’s function which will receive two variables and then return the sum of those variables which then will be printed out on the console.

/**
 * Function which return the sum of two variables
 */

function sum(a, b) {
	return a+b;
}

console.log(sum(1,2)); // the outcome is 3

Here is another example which the function will not return anything but just print the outcome.

/**
 * Function which prints the sum of two variables
 */

function sum(a, b) {
	console.log(a+b);
}

sum(4, 5); // the outcome is 9

Function will be the most frequently used statement in javascript so make sure you have mastered it thoroughly! Lets move on to the next chapter!

Leave a Reply

Be the First to Comment!

avatar
  Subscribe  
Notify of