Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

show example calls for destructuring version
Source Link
ggorlen
  • 59.5k
  • 9
  • 119
  • 174

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.

YouIn ES6, you can also 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';
 ...
}

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.

You can also 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
 ···
}

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';
 ...
}

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';
 ...
}
added link
Source Link
user8554766
user8554766

From ES6/ES2015ES6/ES2015, default parameters isare 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.

You can also 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
 ···
}

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';
 ...
}

From ES6/ES2015, default parameters is 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.

You can also 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
 ···
}

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';
 ...
}

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.

You can also 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
 ···
}

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';
 ...
}
fixed function formatting
Source Link
jonschlinkert
  • 11k
  • 4
  • 45
  • 52

From ES6/ES2015, default parameters is 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.

You can also 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
 ···
}

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';
 ...
}

From ES6/ES2015, default parameters is 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.

You can also 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
 ···
}

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';
 ...
}

From ES6/ES2015, default parameters is 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.

You can also 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
 ···
}

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';
 ...
}
Add simulated default named parameter
Source Link
Dan Dascalescu
  • 153.7k
  • 66
  • 336
  • 422
Loading
deleted 6 characters in body
Source Link
Tomasz Jakub Rup
  • 10.7k
  • 7
  • 52
  • 49
Loading
Add ES2015
Source Link
Boopathi Rajaa
  • 4.7k
  • 2
  • 34
  • 54
Loading
Loading
Loading
Loading
Source Link
Tom Ritter
  • 101.6k
  • 32
  • 143
  • 174
Loading
lang-js

AltStyle によって変換されたページ (->オリジナル) /