1/16/26, 5:42 PM JavaScript Function Definitions
Tutorials References Exercises Sign In
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
JavaScript Function Definitions
❮ Previous Next ❯
Function Declarations and Function
Expressions
JavaScript functions are defined with the function keyword.
JavaScript functions can be defined in different ways.
The most common are function declarations and function expressions.
Function Declarations
A function declaration uses the function keyword and a function name.
Function declarations are loaded before the code runs.
[Link] 1/7
1/16/26, 5:42 PM JavaScript Function Definitions
Tutorials
Example References Exercises Sign In
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
function add(a, b) {
return a + b;
}
add(4, 5);
You can call a function declaration before or after it is written in the code.
Function Expressions
A function expression stores a function inside a variable.
The function can be anonymous (without a name).
Example
const add = function(a, b) {
return a + b;
};
add(4, 5);
Function expressions are executed only when the code reaches them.
Declarations vs Expressions
The difference is when the function becomes available.
Example
add1(4, 5);
[Link] 2/7
1/16/26, 5:42 PM JavaScript Function Definitions
function add1(a, b) {
Tutorials
return a + b; References Exercises Sign In
}
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
Example
add2(4, 5); // Error
const add2 = function(a, b) {
return a + b;
};
In the first example, the function works before it is defined.
In the second example, the function does not exist until the code reaches it.
Why This Happens (Hoisting)
Function declarations are hoisted to the top of their scope.
Function expressions are not hoisted in the same way.
This means:
Function declarations can be called before they appear
Function expressions cannot be called before they are defined
Functions Stored in Variables
A function stored in a variable can be used like any other value.
Example
const myFunction = function() {
return "Hello";
};
let text = myFunction();
[Link] 3/7
1/16/26, 5:42 PM JavaScript Function Definitions
Quiz
Tutorials References Exercises Sign In
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
Which type of function can be called before it is defined in the code?
When to Use Each
Use function declarations for general-purpose functions
Use function expressions when assigning functions to variables
Function expressions are common in callbacks and event handlers
Common Mistakes
Calling a function expression too early
Function expressions must be defined before they are called.
Forgetting the semicolon
Function expressions are part of a statement and should end with ;.
Confusing function names and variable names
In expressions, the variable name is the function reference.
Next Chapter
Next: Arrow Functions
❮ Previous Next ❯
Sign in to track progress
[Link] 4/7
1/16/26, 5:42 PM JavaScript Function Definitions
Tutorials References Exercises Sign In
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
COLOR PICKER
-->
PLUS SPACES
GET CERTIFIED FOR TEACHERS
[Link] 5/7
1/16/26, 5:42 PM JavaScript Function Definitions
Tutorials
BOOTCAMPS
References Exercises
CONTACT US
Sign In
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
[Link] Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
[Link] Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
AngularJS Reference
jQuery Reference
Top Examples Get Certified
HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
[Link] Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate
FORUM ABOUT ACADEMY
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and
learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full
correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies
and privacy policy.
[Link] 6/7
1/16/26, 5:42 PM JavaScript Function Definitions
Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by [Link].
Tutorials References Exercises Sign In
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
[Link] 7/7