How to use default parameters in JavaScript

Using default parameters in JavaScript functions provides fallback values when arguments are not supplied, creating more flexible and robust function interfaces. As the creator of CoreUI with over 25 years of JavaScript development experience, I’ve used default parameters extensively for API methods, configuration functions, and component initialization. From my expertise, the most effective approach is defining default values directly in the function parameter list using ES6 syntax for clean, readable code. This pattern eliminates the need for manual parameter checking and provides clear documentation of expected function behavior.

Define default values directly in function parameters using ES6 syntax to provide fallback values for missing arguments.

function createUser(name = 'Anonymous', age = 18, role = 'user') {
  return {
    name,
    age,
    role,
    id: Math.random().toString(36)
  }
}

console.log(createUser()) // Uses all defaults
console.log(createUser('John')) // Uses defaults for age and role
console.log(createUser('Jane', 25)) // Uses default for role only

Here the function parameters name = 'Anonymous', age = 18, and role = 'user' provide default values when arguments are not passed or are undefined. Default parameters are evaluated at call time, allowing for dynamic expressions and function calls as defaults. You can mix required and optional parameters, but default parameters should come after required ones for best practices.

Best Practice Note:

This is the same approach we use in CoreUI components for configuration options, API methods, and utility functions where flexibility and ease of use are essential. Default parameters make functions more intuitive to use and reduce the need for extensive parameter validation and fallback logic inside function bodies.


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.
What is the difference between typeof and instanceof in JavaScript
What is the difference between typeof and instanceof in JavaScript

How to loop inside React JSX
How to loop inside React JSX

Dealing with Sass Deprecation Warnings in Angular 19
Dealing with Sass Deprecation Warnings in Angular 19

Mastering JavaScript List Comprehension: The Ultimate Guide
Mastering JavaScript List Comprehension: The Ultimate Guide

Answers by CoreUI Core Team