Javascript Function

Function is a block of code that can be called multiple times and reduced development time. It takes one or more input as parameter, does some processing as defined inside the function and return processing result.

Syntax of function

function function_name(parameter1, parameter2){

The number of parameters varies from one function to another.

User Defined functions

This is the general way of defining a function.

A function is defined using function keyword, and the function name should be unique, If a function with the same function name is defined twice then it will return error. We can pass one or more parameter or no parameter in a function according to the requirement. Function statement start and stop with curly braces.

The statement inside function processing some operation on the parameters and return processing result. A function statement ends with output.


function addition(var1, var2){ 
	sum = var1 + var2; 
sumresult = addition(4, 10);


We can call addition function everywhere in the code, where there is need to sum the values. The number of parameters in defining function and call function should be same.

Build In function call

Javascript has rich set of build in functions that make our development so easy. The javascript functions cover all small to small and large to large tasks manipulations.

These are the some examples of javascript build in functions.

parseFloat() - It recognizes the floating number and decimal number format.

var num1 = parseFloat("8344data"); 
alert(num1); // 8344

toString() - It is used to convert a value to a string.

var num =10;
alert(num.toString()); //10
alert(num.toString(16)); // "a"

length() - It returns length of a string.

var str = 'Hello World';