JavaScript Day 11 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation
100 Days of JavaScript Interview Prep: A Beginner’s Journey

Welcome to Day 11–100 Days of JavaScript Interview Prep: A Beginner’s Journey
Embarking on a 100-day adventure focused on learning, practice, and growth sounds like an incredibly rewarding journey.
DAY 11 (April 2nd, 2024) — Function Declaration and Function Expression in JavaScript with examples
Explain the difference between function declarations and function expressions in JavaScript.
Answer :
Function Declarations:
- Function declarations are defined using the
functionkeyword followed by the function name and a block of code enclosed in curly braces.
function sayName() {
console.log("Hello!");
}
console.log(sayName()) // can access 
- These functions are hoisted, it means they are available for use anywhere within their scope, even before the declaration.
console.log(sayName()) // can access even before declaration
function sayName() {
console.log("Hello!");
}Function expressions
Function expressions involve defining a function as part of an expression, typically by assigning it to a variable.
- Function expressions are not hoisted in the same way as function declarations, so they cannot be invoked before they are defined.
console.log(myName()) // can't access before decalaration
const myName =function sayName() {
console.log("Hello!");
}
- These functions may or may not have a name, and if they do, they are only accessible within their containing scope.

Conclusion
- Function declarations are hoisted, while function expressions are not.
- Function declarations can be accessed anywhere within their containing scope, whereas function expressions can only be accessed after they have been defined.
- Function declarations require a name, while function expressions may or may not have a name (anonymous or named)
Examples below anonymous and named
//anonymous
const add = function(x, y) {
return x + y;
};named
const addTwo = function sum(x, y) {
return x + y;
};Happy Coding and Stay Tuned :)
Kind is cool !!!
Share knowledge to gain knowledge
!!!! Be kind and treat people with respect !!
If you find my videos helpful, then please subscribe, Claps and follow to support :)
Cheers
Kirti Kaushal






