Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 1 of 10 · Functions and Arrow Functions
Interview question

What is the difference between function declaration and function expression? Function declaration और function expression में क्या अंतर है?

Answer
AspectDeclarationExpression
Syntaxfunction name() { }const name = function() { }
HoistingFully hoisted - can call before declarationNot hoisted - throws error if called before
NamedAlways namedCan be anonymous or named
When it runsAt parse timeAt runtime
// Function Declaration - hoisted
console.log(greet1()); // 'Hello' - works!
function greet1() {
    return 'Hello';
}

// Function Expression - not hoisted
console.log(greet2()); // ReferenceError: Cannot access 'greet2' before initialization
const greet2 = function() {
    return 'Hello';
};

// Named function expression
const factorial = function fact(n) {
    return n <= 1 ? 1 : n * fact(n - 1);
};

Interview tip: Explain that hoisting means function declarations are moved to the top of their scope at parse time, but function expressions are only initialized when the code runs. This is a common source of bugs.

AspectDeclarationExpression
Syntaxfunction name() { }const name = function() { }
HoistingFully hoisted - पहले call कर सकते हैंHoisted नहीं - error मिलेगी
Namedहमेशा namedAnonymous या named
ExecutionParse time परRuntime पर
// Declaration - hoisted
console.log(greet1()); // 'Hello' - काम करता है!
function greet1() {
    return 'Hello';
}

// Expression - hoisted नहीं
console.log(greet2()); // ReferenceError
const greet2 = function() {
    return 'Hello';
};

// Named function expression
const factorial = function fact(n) {
    return n <= 1 ? 1 : n * fact(n - 1);
};

इंटरव्यू टिप: समझाएं hoisting का मतलब function declarations को scope के top पर move किया जाता है, पर function expressions सिर्फ तब initialize होते हैं जब code run हो।

Was this answer clear?