Skip to main content

Command Palette

Search for a command to run...

JavaScript Function Declaration vs. Function Expressions: Understanding the Differences for Effective Coding

Updated
•3 min read•View as Markdown
JavaScript Function Declaration vs. Function Expressions: Understanding the Differences for Effective Coding
L

Hi, I am Abdul-Qawi and I’m a dedicated Frontend Engineer and Community Manager with over three years of experience in both fields. I specialize in creating seamless user experiences while fostering dynamic digital communities. Throughout my academic and professional journey, I’ve built impactful web projects that drive collaboration and learning while advancing the boundaries of software development. Key Skills -Frontend Development : React.js, Next.js and Tailwind.css -No-Code website development : WordPress, Webflow, Shopify -Community Development, Leadership & Event Organization -Technical Writing: Hashnode (devdotun.hashnode.dev) Career Highlights : -Frontend Engineer at AIESEC in Nigeria -Co-founded SmartLearn, an EdTech platform tailored to Nigeria’s needs -Founded and lead Dev League, a thriving tech community of thousands of members -Contributor at Hashnode and Freecodecamp -Built responsive, scalable web applications (1k+ users) using modern JavaScript frameworks Looking Ahead I’m passionate about leveraging my technical expertise and community-focused approach to deliver innovative projects that enhance customer engagement and user experience. I’m always open to new collaborations, challenges and roles. Let’s Connect Email me at oladotunlaniyan@gmail.com to discuss potential opportunities or chat about tech, community building, or web development.

In JavaScript, functions are fundamental building blocks that allow you to encapsulate reusable code. However, functions can be defined in two distinct ways: through function declarations and function expressions. While they both achieve the same goal of executing code, understanding their differences is crucial for writing clean, efficient, and re-usable code.

Function Declarations :

  • Syntax : "function functionName(parameters) { function body }"

      function addNumbers(num1, num2){
          return num1 + num2;
      }
    
  • Hoisting: Function declarations are hoisted, meaning they are accessible before their declaration in the code. This allows you to call a declared function even before it's defined in the script.

    "Hoisting is a concept in JavaScript is a behavior where variable declarations and function declarations are moved to the top of their containing scope during the compile phase, before the code execution."

  • Scope: Function declarations create their own scope, separate from the global scope. Variables and functions defined within a declared function are not accessible outside it. If this sounds difficult to understand, kindly read this article : https://www.w3schools.com/js/js_scope.asp

  • Naming: Function declarations require a name.

Function Expressions:

  • Syntax: "const functionName = (parameters) => {function body}"

      const greet = function(name) {
          return "Hello, " + name + "!";
      };
    
      console.log(greet("Alice")); // Output: "Hello, Alice!"
    
  • No hoisting: Function expressions are not hoisted, meaning you cannot call them before their definition in the code.

  • Scope: Function expressions inherit the scope in which they are defined. Variables and functions within an expression have access to variables and functions outside it.

Naming: Function expressions can be anonymous (without a name).

FeatureFunction DeclarationFunction Expression
HoistingYesNo
ScopeOwn ScopeInherited Scope
NamingrequiredNot required

When To Use which ?

  • Function declarations:

    • When you need to call a function before its definition.

    • When you want to create a function with its own scope.

    • When you want to create a named function for better readability

  • Function expressions:

    • When you don't need to call a function before its definition.

    • When you want to use a function as a value or pass it as an argument to another function.

    • When you want to create a concise anonymous function.

Best Practices:

  • Use function declarations for globally accessible functions or when hoisting is necessary.

  • Use function expressions for concise anonymous functions or functions passed as arguments.

  • Be consistent in your naming conventions for better code readability.

// Function declaration (hoisted)
function sayHello() {
  console.log("Hello!");
}

sayHello(); // This works!

// Function expression (not hoisted)
const sayGoodbye = () => {
  console.log("Goodbye!");
};

// sayGoodbye(); // This throws an error!

// Anonymous function expression
const add = (a, b) => a + b;

console.log(add(5, 3)); // Prints 8

By understanding the differences between function declarations and expressions, you can make informed decisions about how to define your functions and write more effective and maintainable JavaScript code.

More from this blog

Qawi's Blog

13 posts

Frontend developer, community builder, and technical writer. I share insights on web development, tech communities, and my journey in the tech space. Join me as I explore code and creativity