Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Understanding JavaScript Functions: Types, Scope, Closures, and Advanced Techniques

Understanding JavaScript Functions: Types, Scope, Closures, and Advanced Techniques

This article provides a comprehensive guide to JavaScript functions, covering their various forms, execution contexts, scope chains, arguments handling, return values, the dynamic this keyword, and advanced functional programming techniques such as currying, decoration, and function composition.

The article explains the fundamentals of JavaScript functions, describing how a single function type can be created in multiple forms including function declarations, function expressions, named function expressions, immediately‑invoked function expressions (IIFE), and function objects created with the Function constructor.

It discusses function parameters, the arguments object, default values, and how to manipulate arguments, illustrating these concepts with examples such as:

function add(a, b) {
    return a + b;
}
var result = add(1, 2);
console.log(result); // 3

The concepts of execution contexts, variable objects, and scope chains are introduced, showing how JavaScript resolves identifiers and how lexical scoping works across global, function, and eval code.

Closures are explained as a way for inner functions to retain references to outer‑function variables after the outer function has finished executing, with examples demonstrating how to encapsulate data and create private state.

(function () {
    var secret = 'hidden';
    window.reveal = function () { return secret; };
})();
console.log(reveal()); // 'hidden'

The dynamic nature of the this keyword is covered, including its default binding to the global object, method binding, and how to explicitly set its value using apply and call:

function show() { console.log(this.name); }
var obj = { name: 'Alice' };
show.apply(obj); // 'Alice'

Advanced functional techniques are presented: currying (partial application) is shown with a curryAdd function that returns a new function with a preset argument; function decoration is demonstrated by wrapping a storage function to accept key/value pairs; and function composition is illustrated with a combine helper that chains two functions together.

function curryAdd(a) {
    return function (b) { return a + b; };
}
var add5 = curryAdd(5);
console.log(add5(3)); // 8
function combine(fnA, fnB) {
    return function () {
        var args = Array.prototype.slice.call(arguments);
        var result = fnA.apply(null, args);
        return fnB.call(null, result);
    };
}
var addThenSquare = combine(add, square);
console.log(addThenSquare(2,3)); // 25
About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
JavaScriptfunctionsCurryingclosuresscopeCompositiondecoration

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion