DEV Community

loading...

Fancy Function Parameters of JS

chenge
Ruby, and learn Rust, Go, Elixir, Erlang...
・1 min read

The code is from this post. It makes the params more clear.

function renderList(list, { 
    ordered = true,
    color = '#1e2a2d',
    bgColor = 'transparent'
} = {}) {
    /* ... */
}

// simple use
renderList(['love', 'patience', 'pain'])

// with all arguments
renderList(['one', 'two'], { ordered: true, color: '#c0ffee', bgColor: '#5ad' })

// with only one optional argument (bgColor)
renderList(['one', 'two'], { bgColor: '#5ad' })

Discussion (0)