avatarKirti K

Summary

The web content provides a comprehensive guide on the differences between function declarations and function expressions in JavaScript, essential for interview preparation.

Abstract

The article titled "JavaScript Day 11 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation" is part of a series aimed at beginners preparing for JavaScript interviews. It delves into the nuances of function declarations and function expressions, explaining how function declarations are defined using the function keyword and are hoisted, allowing them to be used before their actual definition in the code. In contrast, function expressions, which are assigned to variables, are not hoisted in the same way and cannot be invoked before they are defined. The guide also distinguishes between named and anonymous function expressions and emphasizes the importance of understanding these concepts for mastering JavaScript basics.

Opinions

  • The author, Kirti Kaushal, emphasizes the value of a structured 100-day learning journey for personal growth and interview preparation.
  • The article suggests that understanding the intricacies of function declarations and expressions is crucial for JavaScript proficiency.
  • Hoisting is highlighted as a key concept that differentiates function declarations from function expressions.
  • The author advocates for sharing knowledge as a means to gain knowledge and encourages readers to be kind and respectful in their interactions.
  • The importance of naming functions, whether in declarations or expressions, is underscored for better code readability and debugging.
  • The article concludes with a call to action for readers to subscribe, clap, and follow to support the author's work, indicating a desire for community engagement and support.

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 -Kirti Kaushal

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 function keyword 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.
JavaScript Day 11 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation

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

Thank you — JavaScript Day 11 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation
Coding Interviews
JavaScript
Javascript Interview
Front End Development
Programming
Recommended from ReadMedium