Answers for "javascript default parameters object destructuring"

2

javascript default parameters object destructuring

You can use "defaults" in destructuring as well:

(function test({a = "foo", b = "bar"} = {}) {
  console.log(a + " " + b);
})();

This is not restricted to function parameters, 
but works in every destructuring expression.
Posted by: Guest on August-21-2021
5

javascript function destructuring

function f() {
  return [1, 2];
}

let a, b; 
[a, b] = f(); 
console.log(a); // 1
console.log(b); // 2
Posted by: Guest on July-29-2020
1

object destructuring default value

let a, b;

[a=5, b=7] = [1];
console.log(a); // 1
console.log(b); // 7
Posted by: Guest on June-03-2020
1

array destructuring in javascript

const array = ['ismail', 'sulman'];
// array destructuring
const [firstElement, secondElement] = array;
console.log(firstElement, secondElement);
const secondArray = ['ismail', 'naeem', 'Mr', 1];
const [firstEl, secondEl, ...array1] = secondArray;
console.log(firstEl, secondEl, array1);
Posted by: Guest on December-12-2020
1

js object destructuring

const { [propName]: identifier } = expression;
Posted by: Guest on June-28-2020
0

js object destructuring with defaults

// Taken from top stack overflow answer

const { dogName = 'snickers' } = { dogName: undefined }
console.log(dogName) // what will it be? 'snickers'!

const { dogName = 'snickers' } = { dogName: null }
console.log(dogName) // what will it be? null!

const { dogName = 'snickers' } = { dogName: false }
console.log(dogName) // what will it be? false!

const { dogName = 'snickers' } = { dogName: 0 }
console.log(dogName) // what will it be? 0!
Posted by: Guest on October-28-2020

Code answers related to "javascript default parameters object destructuring"

Code answers related to "Javascript"

Browse Popular Code Answers by Language