DEV Community

dss99911
dss99911

Posted on • Originally published at dss99911.github.io

JavaScript Functions - Declaration, Hoisting, and Advanced Patterns

Functions are first-class citizens in JavaScript, meaning they can be passed around like any other value. This post covers everything you need to know about JavaScript functions.

Function Declaration

There are several ways to declare functions in JavaScript:

Standard Declaration

function myFunction(a, b) {
    return a * b;
}
Enter fullscreen mode Exit fullscreen mode

Anonymous Function (Function Expression)

const myFunction = function(a, b) {
    return a * b;
};
Enter fullscreen mode Exit fullscreen mode

Function Constructor (Not Recommended)

const myFunction = new Function("a", "b", "return a * b");
Enter fullscreen mode Exit fullscreen mode

Function Hoisting

JavaScript hoists function declarations, allowing you to call functions before they're defined:

// This works!
myFunction(5);  // Returns 25

function myFunction(y) {
    return y * y;
}
Enter fullscreen mode Exit fullscreen mode

Note: Function expressions are NOT hoisted. Only function declarations are.

Self-Invoking Functions (IIFE)

Immediately Invoked Function Expressions execute automatically:

(function() {
    var x = "Hello!!";  // I will invoke myself
    console.log(x);
})();
Enter fullscreen mode Exit fullscreen mode

Arguments Object

Functions have access to an arguments object containing all passed parameters:

function findMax() {
    var max = -Infinity;
    for (var i = 0; i < arguments.length; i++) {
        if (arguments[i] > max) {
            max = arguments[i];
        }
    }
    return max;
}

findMax(1, 123, 500, 115, 44, 88);  // Returns 500
Enter fullscreen mode Exit fullscreen mode

Arguments Properties

arguments.length;  // Number of arguments
arguments[0];      // First argument
Enter fullscreen mode Exit fullscreen mode

The this Keyword

Understanding this is crucial in JavaScript:

In Global Context

function myFunction() {
    return this;
}
myFunction();  // Returns window object (in browser)
Enter fullscreen mode Exit fullscreen mode

In Object Constructor

function Person(firstName, lastName) {
    this.firstName = firstName;  // 'this' refers to the new object
    this.lastName = lastName;
}

var person = new Person("John", "Doe");
Enter fullscreen mode Exit fullscreen mode

Global Scope

Any variable or function declared without an owner belongs to the window object:

var x = 10;          // window.x
function test() {}   // window.test
Enter fullscreen mode Exit fullscreen mode

call() and apply()

These methods allow you to borrow methods from other objects:

call() Method

const person = {
    fullName: function() {
        return this.firstName + " " + this.lastName;
    }
};

const myObject = {
    firstName: "John",
    lastName: "Doe"
};

person.fullName.call(myObject);  // "John Doe"
Enter fullscreen mode Exit fullscreen mode

apply() Method

Similar to call(), but accepts an array of arguments:

Math.max.apply(null, [1, 2, 3]);  // 3

const person = {
    fullName: function(city, country) {
        return this.firstName + " " + this.lastName + ", " + city + ", " + country;
    }
};

person.fullName.apply(myObject, ["Oslo", "Norway"]);
Enter fullscreen mode Exit fullscreen mode

Generator Functions and yield

ES6 introduced generator functions that can pause and resume execution:

Basic Generator

function* generator() {
    console.log(0);
    yield 1;
    yield 2;
    yield 3;
}

let iterator = generator();
iterator.next();  // { value: 1, done: false }
iterator.next();  // { value: 2, done: false }
iterator.next();  // { value: 3, done: false }
iterator.next();  // { value: undefined, done: true }
Enter fullscreen mode Exit fullscreen mode

Fibonacci Generator Example

function* fibonacci() {
    var fn1 = 0;
    var fn2 = 1;
    while (true) {
        var current = fn1;
        fn1 = fn2;
        fn2 = current + fn1;
        var reset = yield current;
        if (reset) {
            fn1 = 0;
            fn2 = 1;
        }
    }
}

var sequence = fibonacci();
console.log(sequence.next().value);     // 0
console.log(sequence.next().value);     // 1
console.log(sequence.next().value);     // 1
console.log(sequence.next().value);     // 2
console.log(sequence.next().value);     // 3
console.log(sequence.next().value);     // 5
console.log(sequence.next().value);     // 8
console.log(sequence.next(true).value); // 0 (reset)
console.log(sequence.next().value);     // 1
Enter fullscreen mode Exit fullscreen mode

Built-in Functions

setTimeout

Execute code after a delay:

setTimeout(function() {
    alert("Hello");
}, 3000);  // 3 seconds
Enter fullscreen mode Exit fullscreen mode

setInterval

Execute code repeatedly:

var id = setInterval(frame, 5);  // Every 5ms

function frame() {
    if (/* test for finished */) {
        clearInterval(id);
    } else {
        /* code to execute */
    }
}
Enter fullscreen mode Exit fullscreen mode

Error Handling

JavaScript uses try-catch blocks similar to Java:

try {
    // Code that might throw an error
    throw "Something went wrong";
} catch (err) {
    console.log(err.name);     // Error name
    console.log(err.message);  // Error message
}

// Throwing different types
throw "Too big";    // Throw a string
throw 500;          // Throw a number
Enter fullscreen mode Exit fullscreen mode

Control Flow

Labels and break

You can label code blocks and break out of them:

list: {
    text += cars[0] + "<br>";
    text += cars[1] + "<br>";
    text += cars[2] + "<br>";
    break list;  // Exit the block
    text += cars[3] + "<br>";  // Not executed
    text += cars[4] + "<br>";
    text += cars[5] + "<br>";
}
Enter fullscreen mode Exit fullscreen mode

Debugging

Console Methods

console.log("Basic logging");

// View all properties of an object
for (var p in theObject) {
    console.log(p);
}
Enter fullscreen mode Exit fullscreen mode

Functions are the heart of JavaScript programming. Understanding these concepts will help you write more elegant and efficient code.


Originally published at https://dss99911.github.io

Top comments (0)