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
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Functions › Default_parameters
Default parameters - JavaScript - MDN Web Docs
Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.
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" }); 
Discussions

Default argument values in JavaScript functions - Stack Overflow
Simple: where "1" is the default value. function abc (arg){ arg=arg===undefined?1:arg; } ... In javascript you can call a function (even if it has parameters) without parameters. More on stackoverflow.com
🌐 stackoverflow.com
With a functin that has two arguments with default values, what is the best way to call this function with the default value for the first argument but with a new value for the second argument?
Using undefined as an argument value is the correct way to let the parameter get its default. Alternatively you can use an object for your optional parameters (similar to what InTheAtticToTheLeft suggested) which is a little more verbose - in a nice, named parameter kind of way - but doesn't require undefineds in place of unspecified parameters. function myFunc({argOne = 'one', argTwo = 'two'} = {}) { console.log(argOne, argTwo); } myFunc({argOne: 'newOne'}) // newOne two myFunc({argTwo: 'newTwo'}) // one newTwo The example above uses destructuring to allow the properties of the object to be seen as individual variables within the function body. The default empty object accounts for the case when no arguments are provided. myFunc() // one two More on reddit.com
🌐 r/learnjavascript
15
4
April 18, 2024
Whats the best way to skip optional parameters?
You can pass 'undefined' instead of the parameter you want to skip. More details here https://stackoverflow.com/questions/8356227/skipping-optional-function-parameters-in-javascript . Also a good practice is to group your parameters in an object and pass just one parameter as an object. In this case you don't need to worry about the order of parameters passed More on reddit.com
🌐 r/learnjavascript
25
6
July 4, 2022
Assigning default value

This isn’t quite correct, at least not anymore. This will also catch false and 0 since || will check for falsy values on the left hand side. You can use ?? nowadays to guard against only null and undefined

More on reddit.com
🌐 r/learnjavascript
25
278
March 23, 2021
🌐
Flexiple
flexiple.com › javascript › javascript-default-parameters
JavaScript Default Parameters - Flexiple
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.
🌐
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.
🌐
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.
🌐
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 ...
Find elsewhere
🌐
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)
🌐
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.
🌐
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.
🌐
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.
🌐
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.
🌐
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....
🌐
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.
🌐
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?