Functions in Javascript

In this tutorial, we are going to have a look at functions in JavaScript; its Declaration and example.

What are the Functions in JavaScript?

Functions in javascript are fully typed object, that you can pass as data to another function also.

  • It can also be Extended and Manipulated.

Function Declaration in JavaScript

/*codeatglance sample of function declaration in javascript*/
function functionName() {

   // body of the function

   // return statement(optional);

Example of Function in JavaScript

/*codeatglance example of functions in javascript*/
function demo(name)
console.log("Name: "+name);//printing the outpt on screen


Name: John

Each function in JavaScript returns a value, if there is no return statement then the function returns undefined.

Example of a function returns undefined

/*codeatglance example of a function returns undefined*/
function withReturn(){
return 100;
function withoutReturn(){



How to pass a value to the function in JavaScript?

/*codeatglance example how to pass a value to the function in Node.js*/
function demo(name) {
console.log("My name :"+ name);


My name :undefined
My name :Lary

Undefined in Javascript: If there is no parameter passed in the javascript function then the returning variable which is assigned the value will return undefined.
Unused in Javascript: If there are many parameters passed in the function then the extras will remain unused.
Anonymous function in javascript: Name of the function in Node.js is optional and such functions are known as an anonymous function.

Example of an anonymous function in Javascript

var sum=function(num1,num2){
return num1+num2;

The output of the Code



Leave a Reply