Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript function destructuring

// destructuring assignment when passing
// an object of properties to a function
function example({userOptions: {query, all = true, sort} = {}, array}) {
  console.log(query, all, sort) // something false ascending
}

// for clarity, the above example is destructured the same as this
function example({userOptions, array}){
  const {query, all = true, sort} = userOptions || {}
}

const userOptions = {query: 'something', all: false, sort: 'ascending'}
const array = []
example({userOptions, array})
Source by developer.mozilla.org #
 
PREVIOUS NEXT
Tagged: #javascript #function #destructuring
ADD COMMENT
Topic
Name
5+9 =