Table of Contents
Back Button
Back to Chapter
Control Statements
No items found.
Function & Events
No items found.
Array and its Types
No items found.
OOP : Object Oriented Programming
No items found.
Javascript Standards
No items found.
No items found.
Javascript : Advanced 1
No items found.
Javascript : Advanced 2
No items found.
Additional JavaScript
No items found.


What is Function?

Function is the Group of Code that does any specific task, It is executed when someone Invokes it (Calls It).

Need Of Function

Function is used to divide a complex problem into small parts and make it easy to read, understand and reusable.

Let's suppose you will drive a car, You inserted the key and the car starts up. (This is exactly like you called the function by inserting the key and function starts the engines, A.C, lights).

In the Same Way, Function Performs in Javascript, You have some lines of code that needs to be executed multiple times throughout the program then the function comes into play

i.e. some lines that need to be executed multiple times are placed inside the function with the name startCar and every time you call the function startCar, the car boots up. in this way function becomes reusable.

Life After Functions

i) Once a function is defined, it can be used over and over again. You can invoke the same function many times in your program, which saves you work.

ii) Another aspect of reusability is that a single function can be used in several different (and separate) programs.

Example: When you need to write a new program, you can go back to your old programs, find the functions you need, and reuse those functions in your new program.

You can also reuse functions that somebody else has written for you.

For Example:  console.log You must Know this is a predefined function that is created by the javascript creators, you use log function to print anything in Console Tab.


A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses ().

The code to be executed, by the function, is placed inside curly brackets: { }

function functionName(parameters) { // statements }


Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).


Now, Lets Understand what parameters do.

Let us suppose you have to put the car to ignition mode i.e everything is started except the engine. when you turn the key then the function should start A.C and Light but not the engine, in this case, you have to use parameters.

If you Want to Start a Car Completely then you will create a function like this:

function startCar(engine,ac,lights) { // statements }

If you Want to Start a Car but not engine then you will create a function like this:

function startCar(ac,lights) { // statements }

Parameters Rules :

Parameter names are included in the Parenthesis ( ) separated by commas (,) : (parameter1, parameter2, ...)

All The Variables declared as a parameter inside Parenthesis ( ), these will be accessible only inside the function.


function startCar(ac,lights) { // lights is accessible here. } // lights is not accessible here.

How to Invoke(Call) a Function

The code inside the function will execute when someone invokes (calls) the function.

There are 3 Ways to Call the Function:

When an event occurs (when a user clicks a Button, types in Text Area, click on any Link, click on any Div).

myFunction(parameters (if any));

By using the name of the function with the parameters (if any) these functions are called on clicking the Elements(Button, Link, Div).

When it is invoked (called) from JavaScript code.

function myFunction(parameters (if any)) { // Statements } myFunction(parameters (if any));

In this way, Functions are declared in the javascript section of the page and they can be called by their name with parameters (if any) from any other function.


Automatically (self invoked).

(function () { // Statements }());

Self invoked Function automatically Run when the Page Loads.

What to Write Inside The Function

You can place any bunch of code that does any specific task to make that code reusable by calling their name.


function startCar(engine,ac,lights) { //startengine //startac //startlights }
Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?