Functions

Functions

JavaScript Functions 

Functions in JavaScript are reusable blocks of code designed to perform a specific task.

1️⃣ Function Declaration

A named function that can be called before it's defined (due to hoisting).

Syntax

function functionName(parameters) { // Code to execute return value; // (optional) }

Example: Add two numbers

function add(a, b) { return a + b; } console.log(add(5, 3)); // Output: 8

📌 Hoisting allows calling add() before defining it.

2️⃣ Function Expression

A function stored in a variable. Not hoisted (cannot be called before declaration).

Example: Multiply numbers

const multiply = function (x, y) { return x * y; }; console.log(multiply(4, 5)); // Output: 20

3️⃣ Arrow Functions (ES6) 🚀

A shorter way to write functions.

Syntax

const functionName = (parameters) => expression;

Example: Square a number

const square = (n) => n * n; console.log(square(6)); // Output: 36

📌 If one parameter, parentheses are optional:

const double = x => x * 2;

📌 If no parameters, use empty parentheses:

const greet = () => "Hello!";

4️⃣ Default Parameters

Assign default values to function parameters.

function greet(name = "Guest") { return `Hello, ${name}!`; } console.log(greet()); // Output: Hello, Guest! console.log(greet("John")); // Output: Hello, John!

5️⃣ Rest Parameters (...)

Allows functions to accept any number of arguments.

function sum(...numbers) { return numbers.reduce((total, num) => total + num, 0); } console.log(sum(2, 3, 4, 5)); // Output: 14

6️⃣ Function Scope & Closures

Functions create local scope.

function outer() { let outerVar = "I am outside!"; function inner() { console.log(outerVar); // Can access outer scope } inner(); } outer(); // Output: I am outside!

📌 Closures allow inner functions to access outer variables even after execution.

7️⃣ Higher-Order Functions

Functions that take another function as an argument or return a function.

function operate(a, b, callback) { return callback(a, b); } console.log(operate(10, 5, (x, y) => x + y)); // Output: 15 console.log(operate(10, 5, (x, y) => x * y)); // Output: 50

8️⃣ Immediately Invoked Function Expressions (IIFE)

A function that runs immediately after definition.

(function () { console.log("I run immediately!"); })();

📌 Useful for isolating variables and avoiding pollution of global scope.

🎯 Summary

Function Declarationfunction greet() {} (hoisted)
Function Expressionconst greet = function() {} (not hoisted)
Arrow Functionconst greet = () => {} (shorter syntax)
Default Parametersfunction greet(name = "Guest") {}
Rest Parametersfunction sum(...numbers) {}
Closures → Functions can remember outer variables
Higher-Order Functions → Functions that take/return other functions
IIFE(function() { console.log("Runs immediately!"); })();

🚀 Now you’re ready to master JavaScript functions! Let me know if you need any help. 😊

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close