0% found this document useful (0 votes)
3 views

Functions

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Functions

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

<!-- !

Functions -->

Definition:

A function in JavaScript is a block of code designed to perform a specific


task.
It is executed when "called"
Functions help to organize code into reusable blocks, making the code more
modular and easier to maintain.

1. Named Functions in JavaScript

Definition:

Named functions are typically declared using the `function` keyword followed
by a function name, a set of parameters, and a block of code to execute.

Syntax of Named Functions


- A named function is declared using the following syntax:

function functionName(parameters) {
// Function body
}

2. Anonymous Function in JavaScript

Definition:

An anonymous function is a function that does not have a name.

Syntax:

function (parameters) {
// Function body
}
3. Function Expression in JavaScript

Definition:

A function expression is a way to define a function in JavaScript where the


function is assigned to a variable. Function expressions can be either named or
anonymous.

Syntax:

let variableName = function(parameters) {


// Function body
};

4. Arrow Functions in JavaScript

Definition:

Arrow functions, introduced in ES6 (ECMAScript 2015), provide a shorter and more
concise syntax for writing functions.

Syntax:

// Single parameter and single expression let


variableName = (parameter) => expression

// Multiple parameters and multiple statements let


variableName = (parameters) => {
// Function body
}
5. Higher-Order Functions in JavaScript

Definition:

A higher-order function is a function that either:


1. Takes one or more functions as arguments (i.e., a callback), or
2. Returns a function as its result.

Syntax:

function higherOrderFunction(callback) {
// Higher-order function body
callback();
}

6. Callback Functions in JavaScript

Definition:
A callback function is a function that is passed as an argument to another
function and is executed after some operation has been completed.

Syntax:

function callbackFunction() {
// Function body
}

function higherOrderFunction(callback) {
callback(); // The callback function is called here
}

- Higher-order functions take or return other functions, making them powerful


tools for abstraction and functional programming.
- Callback functions are passed to other functions to be called later,
commonly used in asynchronous operations.
7. Immediately Invoked Function Expression (IIFE) in JavaScript

Definition:

An Immediately Invoked Function Expression (IIFE) is a JavaScript function


that is executed immediately after it is defined.

Syntax:

(function() {
// Function body
})(); // Function invoked immediately

Example:

(function() {
let message = "IIFE in action!";
console.log(message); // Output: IIFE in
action!
})(); // Immediately executed
// ! how to declare Function

function hello() {
console.log("I am hello function ");
}

hello();

// ! take two numbers and add them inside function

let num1 = 10;


let num2 = 20;

function add() {
console.log(num1 + num2);
}

add();

// ! function with parameters

function add1(a, b) {
console.log(`the addition of ${a} and ${b} is ${a + b}`);
}

add1(20, 50);

// ! function with return value

function sub(a, b) {
let c = a - b;
// console.log(c) return c
}

let result = sub(20, 10);


console.log(result);

function hi(name, msg) {


// console.log(` ${name} , ${msg}`)

let combinedMsg = name.concat(" ", msg);


return combinedMsg;
}

let res = hi("MSD", "thala for a reason");


console.log(res);
// ! Anonymous Function

let anno = function () {


console.log("hello I am anonymous function");
};

anno();

console.log(" ");

let sub2 = function (a, b) {


// console.log(a-b) return a-b;
};

console.log(sub2(20, 10));

console.log(" ");

// ! Arrow Funtion

// ! how to declare arrow Function

let hello = () => {};

let hi = () => {};

let multiply = (a, b) => {


console.log(" i am multiply arrow function");
// console.log(a * b) return a*b;
};

console.log(multiply(3, 6));
let mul = multiply(4, 5);
console.log(mul);

console.log(" ");

// ! Arrow Function In one line

let div = (a, b) => a / b;


let ans5 = div(4, 2);
console.log(ans5);
console.log(" ");
// ! nested function and lexical scopping

let a = 20;
let outer = () => {
let b = 30;

let inner = () => {


var c = 50;

console.log(a + b + c);
};

// console.log(c) //? it will throw error

inner();
};

outer();

// ! 5 HigherOrder and 6. Callback Function..

let hi2 = ()=>{


console.log(" i'm hi function... ")
}

let hello = (a,b,c)=>{

console.log(a)
console.log(b)

c()

console.log(' i am hello function...')

hello(18,'vk', hi2)

hello(7,"msd", ()=>{
console.log('i am the captain')
})

console.log('hello');
// ! 7. IIFE

(function()
{
console.log('i am iife function')
})();

(function(){
console.log('hhhhhhhhh')
})()

You might also like