JavaScript Interview Question #17

What is a function declaration in JavaScript?

JavaScript Fundamentals, Types, Scope & Functions Junior Beginner

Quick Interview Answer

A function declaration defines a named function using the function keyword. Function declarations are hoisted in a way that normally allows them to be called before the declaration appears in the source code.

Detailed Explanation

A function declaration defines a named function using the function keyword. Function declarations are hoisted in a way that normally allows them to be called before the declaration appears in the source code.

Functions are first-class values in JavaScript, so a declared function can also be passed as an argument, stored in another variable, or returned from another function.

Code Example

console.log(add(2, 3)); // 5

function add(a, b) {
    return a + b;
}