A Function is a reusable block of code that performs a specific task. Functions help developers avoid code repetition and improve code reusability.
Functions can:
- Accept input values called Parameters
- Perform calculations or operations
- Return a value
- Be called multiple times
- Function Declaration
- Function Expression
- Anonymous Function
- Constructor Function
- Arrow Function
A Function Declaration is created using the function keyword followed by a function name.
function functionName(){
// Statements
}function.js
function addition(){
var a = 10;
var b = 20;
console.log(a+b);
}
addition();30
-
function→ Keyword used to declare a function. -
addition()→ Function Name. -
The function contains two variables:
- a = 10
- b = 20
-
console.log(a+b)prints their sum. -
addition();calls the function.
A Function Expression is a function assigned to a variable.
var variableName = function(){
// Statements
}functionExpression.js
var disp = function sub(){
var a = 10;
var b = 20;
console.log(a-b);
}
disp();-10
- Function is assigned to variable
disp. subis the internal function name.- The function can be called using:
disp();An Anonymous Function is a function without a name.
var variableName = function(){
}anonymousFunction.js
var disp = function(a,b){
console.log(a/b);
}
disp(10,20);0.5
-
The function has no name.
-
It accepts two parameters:
- a
- b
-
10/20gives:
0.5
A Constructor Function is used to create objects.
It is called using the new keyword.
function Student(name,age){
this.name = name;
this.age = age;
}
var obj = new Student("John",25);constructorFunction.js
var a=0;
var b=0;
var c=0;
var disp = function example(a,b,c){
this.a=a;
this.b=b;
this.c=c;
}
var res = new disp(10,20,30);
console.log(res.a);
console.log(res.b);
console.log(res.c);10
20
30
this.a=astores values inside the object.new disp()creates a new object.res.a,res.b, andres.caccess object properties.
Arrow Function was introduced in ES6 (ECMAScript 2015).
It provides a shorter syntax for writing functions.
var disp = ()=>{
Statements
}var disp = ()=>console.log("Arrow Function");
disp();Arrow Function
arrowFunction.js
var mul = (a,b,c)=>{
var res = a*b*c;
return res;
}
var disp = mul(10,20,30);
console.log(disp);6000
-
Arrow function accepts three parameters:
- a
- b
- c
Calculation:
10 × 20 × 30 = 6000
The value is returned and stored in:
var disp| Function Declaration | Function Expression |
|---|---|
| Declared using function name | Assigned to a variable |
| Can be called before declaration | Cannot be called before declaration |
| Has a function name | May be anonymous |
| Hoisted completely | Partially hoisted |
| Normal Function | Arrow Function |
|---|---|
| Uses function keyword | Uses => operator |
| Has its own this | Does not have its own this |
| Supports constructor | Cannot be used as constructor |
| More verbose | Short and concise |
- Functions improve code reusability.
- A function can accept parameters and return values.
- Function Expressions are assigned to variables.
- Anonymous Functions do not have names.
- Constructor Functions create objects using
new. - Arrow Functions were introduced in ES6.
- Arrow Functions provide shorter syntax.
- Arrow Functions do not have their own
this.
Functions are one of the most important building blocks of JavaScript. They allow developers to organize code into reusable blocks, improve maintainability, and reduce code duplication. JavaScript supports multiple types of functions including Function Declaration, Function Expression, Anonymous Function, Constructor Function, and Arrow Function. Understanding these functions is essential for DOM Manipulation, Event Handling, Object-Oriented Programming, and modern JavaScript development.