How to write a function declaration in JavaScript

Declaring functions is one of the most fundamental concepts in JavaScript programming, especially when building reusable code components. As the creator of CoreUI, a widely used open-source UI library, and with over 25 years of experience in software development, I’ve written thousands of function declarations across countless projects. The most straightforward approach is using the function declaration syntax, which creates named functions that are hoisted and available throughout their scope. This method provides excellent readability and debugging capabilities compared to other function definition patterns.

Use the function keyword followed by a name to create a function declaration with full hoisting support.

function calculateTotal(price, tax) {
  return price + (price * tax)
}

Function declarations are hoisted, meaning they’re available before their definition in the code. The function starts with the function keyword, followed by a descriptive name, parentheses containing parameters, and curly braces containing the function body. The function can be called anywhere in the same scope, even before its declaration, due to JavaScript’s hoisting behavior.

Best Practice Note:

This is the approach we use in CoreUI components for core utility functions that need to be accessible throughout the module. Function declarations are preferred over function expressions when you need hoisting behavior and clear function naming for debugging purposes.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to Center a Button in CSS
How to Center a Button in CSS

How to sort an array of objects by string property value in JavaScript
How to sort an array of objects by string property value in JavaScript

How to Achieve Perfectly Rounded Corners in CSS
How to Achieve Perfectly Rounded Corners in CSS

How to compare dates with JavaScript
How to compare dates with JavaScript

Answers by CoreUI Core Team