Why JavaScript Functions Matter
If you've spent any time writing JavaScript, you already know that functions are the backbone of everything you build. They let you package up logic once and reuse it as many times as you need, instead of copying and pasting the same console.log statements over and over. But here's where a lot of beginners get tripped up: JavaScript actually gives you multiple ways to create a function, and each one behaves a little differently under the hood.
In this tutorial, we're going to break down two of the most common approaches: normal function declarations and function expressions. By the end, you'll understand not just the syntax differences, but the behavioral differences that can save you from confusing bugs down the road.
What Is a Normal Function Declaration?
A function declaration (sometimes just called a "normal function") is the classic way to define a function in JavaScript. It starts with the function keyword, followed by a name, a set of parentheses for parameters, and a code block wrapped in curly braces.
function greetUser(name) {
console.log('Hello, ' + name + '!');
}
greetUser('Alex');Notice how the function has a name (greetUser) and can be called simply by referencing that name. This is the most straightforward way to define reusable logic, and it's usually the first syntax new developers learn.
Calling Functions Multiple Times
The real power of a function declaration shows up when you need to run the same logic repeatedly with different inputs. Instead of writing five separate console.log statements, you write one function and call it as many times as needed:
greetUser('Alex');
greetUser('Jamie');
greetUser('Taylor');This is a core principle of clean, maintainable code — write the logic once, then reuse it wherever you need it.
What Is a Function Expression?
A function expression looks similar at first glance, but there's a key difference: the function is assigned to a variable rather than declared with its own name.
const greetUser = function(name) {
console.log('Hello, ' + name + '!');
};
greetUser('Alex');Here, the function itself is anonymous (it has no name of its own), and it's stored inside the greetUser variable. You call it the same way you would call a normal function, but structurally, JavaScript treats it very differently.
Arrow Functions: A Modern Twist on Expressions
Once you're comfortable with function expressions, it's a short jump to arrow functions, an ES6 syntax that makes expressions shorter and easier to read. Here's the same example rewritten as an arrow function:
const greetUser = (name) => {
console.log('Hello, ' + name + '!');
};And if your function body is a single expression, you can strip away the braces, the return keyword, and even the parentheses around a single parameter:
const greetUser = name => console.log('Hello, ' + name + '!');This is one of the reasons arrow functions have become so popular in modern JavaScript — they reduce boilerplate and keep your code concise, especially when working with array methods like map() and filter().
The Critical Difference: Hoisting
This is where things get interesting — and where a lot of developers get caught off guard. JavaScript has a behavior called hoisting, which affects how and when your functions become available in your code.
Function Declarations Are Hoisted
Because normal function declarations are processed before the rest of your code runs, you can actually call them before they appear in your file:
sayHi(); // Works fine!
function sayHi() {
console.log('Hi there!');
}This works because JavaScript essentially "hoists" the entire function declaration to the top of its scope during the compilation phase.
Function Expressions Are Not Hoisted the Same Way
Function expressions behave differently. Since the function is assigned to a variable, only the variable declaration is hoisted — not the function assignment itself. That means calling it too early results in an error:
sayHi(); // Error: sayHi is not a function
const sayHi = function() {
console.log('Hi there!');
};Understanding this distinction is essential for debugging real-world code. If you've ever seen a mysterious "is not a function" error, this is often the culprit.
Functions vs. Methods: A Quick Clarification
Once you understand normal functions and function expressions, it's worth touching on a related concept: methods. A method is simply a function that lives inside an object and is called using dot notation.
const user = {
name: 'Alex',
greet: function() {
console.log('Hi, I am ' + this.name);
}
};
user.greet();Built-in methods like toUpperCase() on strings work the same way — they're just functions attached to an object (or in this case, a string's prototype) that you invoke with dot notation.
Returning Values vs. Logging Values
Another common point of confusion is the difference between displaying a value with console.log() and returning a value with the return keyword.
function add(a, b) {
return a + b;
}
const total = add(5, 3);
console.log(total); // 8If a function doesn't explicitly return anything, it will return undefined by default. This is a subtle but important detail — a function that only logs a value to the console can't have that value stored or reused elsewhere in your program. If you need to use the result later, you must use return.
Parameters vs. Arguments
One more foundational concept worth mastering: the difference between parameters and arguments. Parameters are the placeholder names listed in a function's definition, while arguments are the actual values you pass in when calling the function.
function greet(name) { // 'name' is the parameter
console.log(`Hello, ${name}`);
}
greet('Jordan'); // 'Jordan' is the argumentIf you call a function without providing an argument, the parameter will simply be undefined inside the function body — a common source of bugs if you're not careful.
When Should You Use Each Type?
- Use function declarations when you want hoisting benefits, such as organizing helper functions at the bottom of a file while calling them earlier in your code.
- Use function expressions when you want to control exactly when a function becomes available, or when passing functions as arguments to other functions (like callbacks).
- Use arrow functions when you want concise syntax, especially for short callbacks passed to methods like
map(),filter(), andsort().
Featured Video
Watch the full lecture walkthrough below, where these concepts are demonstrated step-by-step with live coding examples.
Bringing It All Together
Understanding the difference between normal function declarations and function expressions isn't just an academic exercise — it directly impacts how you structure your code, avoid hoisting-related bugs, and choose the right tool for the job. These concepts also lay the groundwork for more advanced topics like arrow functions, object methods, and even similar patterns you'll encounter in Python with lambda functions, map(), and filter().
If you want to go deeper into JavaScript fundamentals — including arrow functions, return values, object literals, and how functions compare to methods — the Full Stack Python Architect: Flask + React + Vibe Coding course walks through each of these concepts hands-on, building the foundation you'll need before moving into full-stack development with Flask and React.
Key Takeaways
- Function declarations are hoisted; function expressions are not.
- Function expressions store an anonymous function inside a variable.
- Arrow functions are a concise form of function expressions, ideal for short callbacks.
- Use
returnwhen you need to reuse a function's output; useconsole.log()only for display purposes. - Parameters are placeholders in a function definition; arguments are the actual values passed in.