Functions
Functions
Functions -->
Definition:
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.
function functionName(parameters) {
// Function body
}
Definition:
Syntax:
function (parameters) {
// Function body
}
3. Function Expression in JavaScript
Definition:
Syntax:
Definition:
Arrow functions, introduced in ES6 (ECMAScript 2015), provide a shorter and more
concise syntax for writing functions.
Syntax:
Definition:
Syntax:
function higherOrderFunction(callback) {
// Higher-order function body
callback();
}
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
}
Definition:
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();
function add() {
console.log(num1 + num2);
}
add();
function add1(a, b) {
console.log(`the addition of ${a} and ${b} is ${a + b}`);
}
add1(20, 50);
function sub(a, b) {
let c = a - b;
// console.log(c) return c
}
anno();
console.log(" ");
console.log(sub2(20, 10));
console.log(" ");
// ! Arrow Funtion
console.log(multiply(3, 6));
let mul = multiply(4, 5);
console.log(mul);
console.log(" ");
let a = 20;
let outer = () => {
let b = 30;
console.log(a + b + c);
};
inner();
};
outer();
console.log(a)
console.log(b)
c()
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')
})()