bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/JavaScript/Objects, Classes, and Advanced Patterns
JavaScript•Objects, Classes, and Advanced Patterns

JavaScript Function Definitions

Function Definitions vs Function Declarations

Function definition is a general term for defining a function.

Function declaration is one specific way to define a function.

Examples of function definitions include

  • Function declarations
  • Function expressions
  • Arrow functions

Example

// Function Declaration
function myFunction(x, y) {
 return x * y;
}
// Function Expression (Named)
const myFunction = function name(x, y) {
 return x * y;
};
// Function Expression (Anonymous)
const myFunction = function (x, y) {
 return x * y;
};
// Arrow Function
const myFunction = (x, y) => x * y;
// Function Constructor
const myFunction = new Function("x", "y", "return x * y");
// Object Method
const obj = {
 myFunction(x, y) {
 return x * y;
 },
};

Function Declarations

Earlier in this tutorial, you learned that functions are declared with the following syntax:

function
functionName
( parameters ) {
 // code to be executed
}

A function declaration uses the function keyword and a function name .

Examples

function myFunction(a, b) {return a * b}

Declared functions are not executed immediately. They are "saved for later use", and will be executed later, when they are invoked (called upon).

You can call a function before or after it is declared in the code.

Examples

let x = myFunction(4, 3);
function myFunction(a, b) {
  return a * b;
}

Function Expressions

A function expression stores a function inside a variable.

The function can be anonymous (without a name).

Function expressions are executed only when the code reaches them.

Example

const x = function (a, b) {return a * b};

After a function expression has been stored in a variable, the variable can be used as a function:

Example

const x = function (a, b) {return a * b};
let z = x(4, 3);

Hoisting

Function declarations are hoisted to the top of their scope.

Function expressions are not hoisted in the same way.

Function declarations can be called before they are defined .

Function expressions can not be called before they are defined .

Key Differences

Function DeclarationFunction Expression
Syntax: A statement starting with the function keyword, followed by a required name.Syntax: A part of an expression or assignment, and can be anonymous (without a name).
Hoisting: The function is moved to the top of its scope before code execution, allowing you to call it from anywhere within that code scope, even before its definition.Hoisting: The variable holding the function might be hoisted (depending on var, let, or const), but the function assignment itself is not. You can only call it after it has been defined in the code scope.
Flexibility: Primarily used for general-purpose functions that need to be available globally or in a specific scope.Flexibility: Can be used in various contexts, such as arguments to other functions (callbacks), immediately invoked function expressions, and assigned to object properties.

Functions Stored in Variables

A function stored in a variable can be used like any other value:

Example

function myFunction(a, b) {
  return a * b;
}
let x = myFunction(4, 3);

JavaScript functions can be used in expressions:

Example

function myFunction(a, b) {
  return a * b;
}
let x = myFunction(4, 3) * 2;

The Function() Constructor

Functions can also be defined with the JavaScript Function() constructor , but this is rarely used.

Example

const myFunction = new Function("a", "b", "return a * b");
let x = myFunction(4, 3);

You actually don't have to use the function constructor. The example above is the same as writing:

Example

const myFunction = function (a, b) {return a * b};
let x = myFunction(4, 3);

Most of the time, you can avoid using the new keyword in JavaScript.

Functions are Objects

The typeof operator in JavaScript returns function for functions.

But, JavaScript functions can best be described as objects .

JavaScript functions have both properties and methods .

The arguments.length property returns the number of arguments received by the function:

Example

function multiply(a, b) {
  return arguments.length;
}
// Returns 0 multiply(); // Returns 2 multiply(a, b);

The toString() method returns the function as a string:

Example

function multiply(a, b) {
  return a * b;
}
let text = multiply.toString();

Previous

JavaScript Iterables

Next

JavaScript Object Properties

This chapter

Overview
123

Lessons

539m

Read time

1. JavaScript Objects2. JavaScript Temporal Tutorial3. JavaScript Loops4. JavaScript Advanced Functions5. JavaScript Objects6. JavaScript Temporal Intro7. JavaScript Iterables8. JavaScript Function Definitions9. JavaScript Object Properties10. JavaScript Temporal vs Date11. JavaScript Iterators12. JavaScript Callbacks13. JavaScript Object Methods14. JavaScript Temporal Duration15. JavaScript Generators16. The JavaScript this Keyword17. JavaScript this Keyword18. JavaScript Temporal Instant19. JavaScript Function call()20. JavaScript Display Objects21. JavaScript PlainDateTime22. JavaScript Function apply()23. JavaScript Object Constructors24. JavaScript Temporal PlainDate25. JavaScript Function bind()26. JavaScript PlainYearMonth27. Self-Invoking Functions28. JavaScript PlainMonthDay29. JavaScript Closures30. JavaScript Temporal PlainTime31. JavaScript Objects - Advanced32. JavaScript ZonedDateTime33. JavaScript Object Definitions34. JavaScript Temporal Now35. this in JavaScript Objects36. JS Temporal Arithmetic37. JavaScript Object Iterations38. JS Temporal Differences39. JavaScript Object Accessors40. JS Temporal Compare41. JavaScript Object Management42. JavaScript Temporal Conversion43. JavaScript Object Protection44. Temporal Display Formats45. JavaScript Object Prototypes46. JavaScript Temporal Mistakes47. JavaScript Classes48. Migrate from Date to Temporal49. JavaScript Class Inheritance50. Temporal Standards51. JavaScript Static Methods52. Asynchronous JavaScript53. Asynchronous Programming54. JavaScript Timeouts55. JavaScript Callbacks56. JavaScript Promises57. JavaScript async and await58. JavaScript fetch API59. Debugging Async JavaScript60. JavaScript Modules61. JavaScript Modules Export62. JavaScript Modules Import63. JS Module Namespace64. JavaScript Dynamic Modules65. JavaScript Meta Programming66. JavaScript Reflect67. JavaScript Proxy68. JavaScript Typed Arrays69. Typed Array Methods70. JavaScript ArrayBuffer71. JavaScript DataView72. JavaScript Atomics73. JavaScript HTML DOM Navigation74. JavaScript HTML DOM Elements (Nodes)75. JavaScript HTML DOM Collections76. JavaScript HTML DOM Node Lists77. JavaScript Window - The Browser Object Model78. JavaScript Window Screen79. JavaScript Window Location80. JavaScript Window History81. JavaScript Window Navigator82. JavaScript Popup Boxes83. JavaScript Timing Events84. JavaScript Cookies85. Web APIs - Introduction86. JavaScript Fetch API87. Web Geolocation API88. Web History API89. Pointer Events API90. Web Storage API91. JavaScript Validation API92. Web Workers API93. AJAX Introduction94. AJAX - The XMLHttpRequest Object95. AJAX - XMLHttpRequest96. AJAX - Server Response97. AJAX XML Example98. AJAX PHP Example99. AJAX ASP Example100. AJAX Database Example101. XML Applications102. JavaScript JSON103. JSON Syntax104. JSON vs XML105. JSON Data Types106. JSON.parse()107. JSON.stringify()108. JSON Object Literals109. JSON Array Literals110. JSON PHP111. JSON HTML112. JSONP113. JavaScript / jQuery DOM Selectors114. JavaScript / jQuery HTML Elements115. JavaScript / jQuery CSS Styles116. JavaScript / jQuery HTML DOM117. JavaScript Graphics118. HTML Canvas119. Plotly.js120. Chart.js121. Google Chart122. D3.js123. JavaScript Interview Preparation

On this page

Function Definitions vs Function DeclarationsFunction DeclarationsFunction ExpressionsHoistingKey DifferencesFunctions Stored in VariablesThe Function() ConstructorFunctions are Objects