Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

JavaScript Functions and Scope

Lesson 20 of 34 in the free Web Technologies notes on Siksha Sarovar, written by Rohit Jangra.

JavaScript Functions and Scope

Function Declarations

// Function declaration (hoisted)
function greet(name) {
    return "Hello, " + name + "!";
}
console.log(greet("Alice")); // Hello, Alice!

// Function expression (not hoisted)
const add = function(a, b) {
    return a + b;
};

// Arrow function (ES6)
const multiply = (a, b) => a * b;

// Arrow function with body
const divide = (a, b) => {
    if (b === 0) return null;
    return a / b;
};

Parameters and Arguments

// Default parameters
function power(base, exp = 2) {
    return base ** exp;
}
power(3);    // 9
power(3, 3); // 27

// Rest parameters
function sum(...numbers) {
    return numbers.reduce((acc, n) => acc + n, 0);
}
sum(1, 2, 3, 4); // 10

// Destructuring parameters
function display({ name, age }) {
    console.log(`${name} is ${age}`);
}
display({ name: "Bob", age: 30 }); // Bob is 30

Scope

// Global scope
let globalVar = "I'm global";

function outer() {
    // Function scope
    let outerVar = "I'm in outer";

    function inner() {
        // Block scope
        let innerVar = "I'm in inner";
        console.log(globalVar);  // accessible
        console.log(outerVar);   // accessible (closure)
        console.log(innerVar);   // accessible
    }

    inner();
    // console.log(innerVar); // ERROR: not accessible
}

Closures

function makeCounter() {
    let count = 0;          // private variable
    return function() {
        count++;
        return count;
    };
}

const counter = makeCounter();
console.log(counter()); // 1
console.log(counter()); // 2
console.log(counter()); // 3

Immediately Invoked Function Expression (IIFE)

(function() {
    const secret = "hidden";
    console.log("IIFE runs immediately");
})();
// secret is not accessible outside

Higher-Order Functions

// Function that takes a function as argument
function doTwice(fn) {
    fn();
    fn();
}
doTwice(() => console.log("Hello"));

// Function that returns a function
function multiplier(factor) {
    return n => n * factor;
}
const double = multiplier(2);
console.log(double(5)); // 10

this Keyword

Contextthis value
Global scopewindow (browser)
Object methodThe object
Arrow functionInherited from surrounding scope
Event handlerThe element
Key Takeaway: Functions are first-class citizens in JavaScript. Understand closures for data encapsulation, arrow functions for concise syntax, and this binding to write correct object-oriented code.