From ES6/ES2015, default parameters are in the language specification.

function read_file(file, delete_after = false) {
  // Code
}

just works.

Reference: Default Parameters - MDN

Default function parameters allow formal parameters to be initialized with default values if no value or undefined is passed.

In ES6, you can simulate default named parameters via destructuring:

// the `= {}` below lets you call the function without any parameters
function myFor({ start = 5, end = 1, step = -1 } = {}) { // (A)
    // Use the variables `start`, `end` and `step` here
    ยทยทยท
}

// sample call using an object
myFor({ start: 3, end: 0 });

// also OK
myFor();
myFor({});

Pre ES2015,

There are a lot of ways, but this is my preferred method โ€” it lets you pass in anything you want, including false or null. (typeof null == "object")

function foo(a, b) {
  a = typeof a !== 'undefined' ? a : 42;
  b = typeof b !== 'undefined' ? b : 'default_b';
  ...
}
Answer from Tom Ritter on Stack Overflow
Top answer
1 of 16
3597

From ES6/ES2015, default parameters are in the language specification.

function read_file(file, delete_after = false) {
  // Code
}

just works.

Reference: Default Parameters - MDN

Default function parameters allow formal parameters to be initialized with default values if no value or undefined is passed.

In ES6, you can simulate default named parameters via destructuring:

// the `= {}` below lets you call the function without any parameters
function myFor({ start = 5, end = 1, step = -1 } = {}) { // (A)
    // Use the variables `start`, `end` and `step` here
    ยทยทยท
}

// sample call using an object
myFor({ start: 3, end: 0 });

// also OK
myFor();
myFor({});

Pre ES2015,

There are a lot of ways, but this is my preferred method โ€” it lets you pass in anything you want, including false or null. (typeof null == "object")

function foo(a, b) {
  a = typeof a !== 'undefined' ? a : 42;
  b = typeof b !== 'undefined' ? b : 'default_b';
  ...
}
2 of 16
636
function read_file(file, delete_after) {
    delete_after = delete_after || "my default here";
    //rest of code
}

This assigns to delete_after the value of delete_after if it is not a falsey value otherwise it assigns the string "my default here". For more detail, check out Doug Crockford's survey of the language and check out the section on Operators.

This approach does not work if you want to pass in a falsey value i.e. false, null, undefined, 0 or "". If you require falsey values to be passed in you would need to use the method in Tom Ritter's answer.

When dealing with a number of parameters to a function, it is often useful to allow the consumer to pass the parameter arguments in an object and then merge these values with an object that contains the default values for the function

function read_file(values) {
    values = merge({ 
        delete_after : "my default here"
    }, values || {});

    // rest of code
}

// simple implementation based on $.extend() from jQuery
function merge() {
    var obj, name, copy,
        target = arguments[0] || {},
        i = 1,
        length = arguments.length;

    for (; i < length; i++) {
        if ((obj = arguments[i]) != null) {
            for (name in obj) {
                copy = obj[name];

                if (target === copy) {
                    continue;
                }
                else if (copy !== undefined) {
                    target[name] = copy;
                }
            }
        }
    }

    return target;
};

to use

// will use the default delete_after value
read_file({ file: "my file" }); 

// will override default delete_after value
read_file({ file: "my file", delete_after: "my value" }); 
๐ŸŒ
Flexiple
flexiple.com โ€บ javascript โ€บ javascript-default-parameters
JavaScript Default Parameters - Flexiple
June 6, 2024 - JavaScript default parameters allow you to initialize function parameters with default values if no arguments are passed or if `undefined` is passed, similar to how a ternary operator provides a default value based on a condition.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-set-default-parameter-value-in-javascript-functions
How to set default parameter value in JavaScript functions ? - GeeksforGeeks
July 12, 2025 - Now we will assign the default values to the variables in its definition only. If no value is passed to the function will assign the newly defined default values and use that value when function is executed.
๐ŸŒ
CoreUI
coreui.io โ€บ answers โ€บ how-to-pass-default-parameters-to-a-function-in-javascript
How to pass default parameters to a function in JavaScript ยท CoreUI
November 5, 2025 - The function signature defines default values using the assignment operator = after each parameter name. When the function is called without arguments or with undefined values, JavaScript automatically uses the default values.
๐ŸŒ
W3Schools
w3schools.com โ€บ howto โ€บ howto_js_default_parameters.asp
How To Set Default Parameter Values for JavaScript Functions
ECMAScript 2015 allows default parameter values in the function declaration: function myFunction (x, y = 2) { // function code } Try it Yourself ยป ยท Read more about functions in our JavaScript Function Tutorial.
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ default-parameters
JavaScript Default Parameters
The default value of y is set to the x parameter. The default value of z is the sum of x and y. So when sum() is called without any arguments, it uses these default values, leading to the calculation 1 + 1 + 2 = 4. Hence, the output is 4. ... We can also pass a function as a default value in ...
๐ŸŒ
JavaScript Tutorial
javascripttutorial.net โ€บ home โ€บ javascript tutorial โ€บ javascript default parameters
The Beginner's Guide to JavaScript Default Parameters
November 15, 2024 - ES6 provides you with an easier way to set the default values for the function parameters like this: function fn(param1=default1, param2=default2,..) { }Code language: JavaScript (javascript)
Find elsewhere
๐ŸŒ
GitHub
googlechrome.github.io โ€บ samples โ€บ default-parameters-es6
Default parameters (ES2015) Sample
Default parameters allow formal function parameters to be initialized with default values if no value (or undefined) is supplied.
๐ŸŒ
Web Reference
webreference.com โ€บ javascript โ€บ es6 โ€บ default-parameters
Default Parameters in ES6 JavaScript
name is the parameter and "John" ... of the name parameter. Default parameters in JavaScript allow us to specify a default value for function parameters, in case no value is provided when the function is called....
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ understanding-default-parameters-in-javascript
Understanding Default Parameters in JavaScript | DigitalOcean
August 27, 2021 - In ECMAScript 2015, default function parameters were introduced to the JavaScript language. These allow developers to initialize a function with default values if the arguments are not supplied to the function call. Initializing function parameters in this way will make your functions easier to read and less error-prone, and will provide default behavior for your functions.
๐ŸŒ
Go Make Things
gomakethings.com โ€บ how-to-set-default-function-arguments-with-vanilla-js
How to set default function arguments with vanilla JS | Go Make Things
When you define your function parameters, add = 'default value' for any one that you want to have a default if not defined.
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ faq โ€บ how-to-set-default-parameter-value-for-a-javascript-function.php
How to Set Default Parameter Value for a JavaScript Function
However, it is often useful to specify a different default value for the parameter. Since ES6, you can simply use the assign (=) operator to set a default value for a function parameter in JavaScript.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ javascript โ€บ examples โ€บ set-a-default-parameter-value-for-a-function
JavaScript Program to Set a Default Parameter Value For a Function | Vultr Docs
December 19, 2024 - This example sets Visitor as the default value for name. If no argument is provided when greet() is called, name defaults to 'Visitor'. Extend the use of default parameters to functions with multiple parameters. Observe how JavaScript handles defaults when some arguments are still provided.
๐ŸŒ
Vue School
vueschool.io โ€บ lessons โ€บ learn-how-to-set-default-parameter-values-in-javascript-functions
Learn how to set default parameter values in JavaScript functions
Prior to ES6, it wasn't possible to set default parameter values when defining our functions in JavaScript. In this lesson, we learn how to create default values to our function parameters. We also see a few examples of how this can come in handy in our applications.
๐ŸŒ
Medium
medium.com โ€บ @rabailzaheer โ€บ demystifying-default-parameters-in-javascript-functions-9f2326479e36
Demystifying Default Parameters in JavaScript Functions
September 23, 2023 - These values come into play when the function is called, and the corresponding argument is missing or undefined. In such cases, the default value is used as a fallback, ensuring that the function continues to execute without errors. In JavaScript, default parameters were introduced in ECMAScript 6 (ES6) and have since become a standard feature in modern JavaScript.
๐ŸŒ
CodeHS
codehs.com โ€บ tutorial โ€บ 13737
Tutorial: Default Parameters in JavaScript | CodeHS
Click on one of our programs below to get started coding in the sandbox
Top answer
1 of 6
382

In javascript you can call a function (even if it has parameters) without parameters.

So you can add default values like this:

function func(a, b){
   if (typeof(a)==='undefined') a = 10;
   if (typeof(b)==='undefined') b = 20;

   //your code
}

and then you can call it like func(); to use default parameters.

Here's a test:

function func(a, b){
   if (typeof(a)==='undefined') a = 10;
   if (typeof(b)==='undefined') b = 20;

   alert("A: "+a+"\nB: "+b);
}
//testing
func();
func(80);
func(100,200);
2 of 6
149

ES2015 onwards:

From ES6/ES2015, we have default parameters in the language specification. So we can just do something simple like,

function A(a, b = 4, c = 5) {
}

or combined with ES2015 destructuring,

function B({c} = {c: 2}, [d, e] = [3, 4]) {
}

For detailed explanation,

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/default_parameters

Default function parameters allow formal parameters to be initialized with default values if no value or undefined is passed.

Pre ES2015:

If you're going to handle values which are NOT Numbers, Strings, Boolean, NaN, or null you can simply use

(So, for Objects, Arrays and Functions that you plan never to send null, you can use)

param || DEFAULT_VALUE

for example,

function X(a) {
  a = a || function() {};
}

Though this looks simple and kinda works, this is restrictive and can be an anti-pattern because || operates on all falsy values ("", null, NaN, false, 0) too - which makes this method impossible to assign a param the falsy value passed as the argument.

So, in order to handle only undefined values explicitly, the preferred approach would be,

function C(a, b) {
  a = typeof a === 'undefined' ? DEFAULT_VALUE_A : a;
  b = typeof b === 'undefined' ? DEFAULT_VALUE_B : b;
}
๐ŸŒ
Greenroots
blog.greenroots.info โ€บ why-use-javascript-function-default-parameters
Why use default parameters in JavaScript functions?
October 30, 2023 - The method emp sets up an empty array as the default value for the destructured parameters, dept and salary. So, when you pass an empty array to the function, it will take the default values for the dept and salary. ... You can override default values by passing the new value as an array element. ... JavaScript's default function parameters are amazing, isn't it?