Extract Functions Arguments using Destructure in JavaScript
ES6 Destructuring is terrific at extracting value from your arguments. So the next time you see the array bracket notation, just swap them out and use the destructuring syntax instead ๐
function omelette(...args) {
// โ Old Way
const egg = args[0];
const cheese = args[1];
// โ
Better Way with Destructuring
const [egg, cheese] = args;
}
omelette('๐ฅ', '๐ง');
Breaking down the code
The first thing weโre doing is collecting all our arguments into an array.
// Step 1:
(...args)
args // [ '๐ฅ', '๐ง' ]
Next, weโre assigning them to our variables using array destructuring.
// Step 2:
const [egg, cheese] = args;
egg; // '๐ฅ'
cheese; // '๐ง'
Understanding the arguments
Object
There's been some confusion on the syntax. I think it's because of the arguments objects. So I'm going to try to explain it. In every function, there is a built-in arguments
object. The arguments
object is an Array-like object that corresponds to the arguments passed into a function.
function omelette() {
console.log(arguments); // { 0: '๐ฅ', 1: '๐ง' }
}
omelette('๐ฅ', '๐ง');
โ๏ธAs you can see the arguments
is not an array. It is an Array-like object. To convert this into a real array, I can use the ...
spread syntax.
function omelette() {
var args = [...arguments];
console.log(args); // [ '๐ฅ', '๐ง' ]
}
omelette('๐ฅ', '๐ง');
Notice my function is NOT accepting any parameters, yet my arguments
object exists. I know it's confusing cause I named it args
. So let's make it crystal clear and check out a function that is passing in a parameter vs the arguments
object.
function food(egg) {
egg; // '๐ฅ'
arguments; // { 0: '๐ฅ', 1: '๐' }
}
food('๐ฅ', '๐');
The term Parameter vs Argument
I always thought these terms were interchangeable. Then I realize there is a language difference.
Parameter: is the variable in the function declaration. It is part of the function signature when you create it.
To use in a sentence, I'd say: "This function is accepting the name
parameter"
function sayHi(name) {
// ๐ parameter
}
Argument: is the actual value of the variable being passed to the function when it is called.
To use in a sentence, I'd say: "I'm passing samantha
in this function"
sayHi('samantha'); // ๐ argument
Here's how I remember it. The "P" in Parameter stands for the Placeholder in the function declaration. The "A" in Argument stands for the the Actual value of the function.
Rest Parameters vs Arguments object
Let's start by explaining what are Rest Parameters:
The rest parameter syntax allows us to represent an indefinite number of arguments as an array.
Rest Parameters collects individual arguments that you pass into a function and returns an array
function cook(...ingredients) { // ๐ Have to accept the parameters
return ingredients;
// [ '๐ง', '๐ฅ' ] ๐ Returns an array
}
cook('๐ง', '๐ฅ'); // ๐ Passing the arguments
However, this is different from the arguments
object. Notice I didn't have to pass the arguments in the parameters. Every non-arrow function created in JavaScript has a local arguments
object. It's also the reason, why you don't want to name using arguments
because you will overwrite it.
function cook() { // ๐ NOT accepting any parameters
return arguments;
// { '0': '๐ง', '1': '๐ฅ' } ๐ Returns an "arguments" object
}
cook('๐ง', '๐ฅ'); // ๐ Passing the arguments
The best practice is to avoid the arguments
object, instead you should use the rest parameters. It's the reason why ES6 introduced the Rest Parameters to make it easier for JavaScript developers that need to access and make it easier to work with an indefinite number of arguments ๐
Arguments best practices
There are some best practices of using Function Arguments that was indicated from AirBnb's JavaScript Style Guide:
Never name a parameter arguments. This will take precedence over the arguments object that is given to every function scope.
// bad
function foo(name, options, arguments) {
// ...
}
// good
function foo(name, options, args) {
// ...
}
Never use arguments, opt to use rest syntax ... instead
Why? ...
is explicit about which arguments you want pulled. Plus, rest arguments are a real Array, and not merely Array-like like arguments.
// bad
function foo() {
const args = Array.prototype.slice.call(arguments);
}
// good
function foo(...args) {
}
Community Input
Setting Default Value
@lukeshiru: You can even set default values in the header.
function omelette(...[egg = '๐ณ', cheese = '๐ฎ']) {
egg; // '๐ณ'
cheese; // '๐ฎ'
}
omelette(); // ๐ NOT passing any value
Destructuring Rest Parameters
@lukeshiru: You can also do it like this.
function omelette(...[egg, cheese]) {
egg; // '๐ฅ'
cheese; // '๐ง'
}
omelette('๐ฅ', '๐ง');
โ๏ธ Let me just break down what @lukeshiru
is doing here cause it might look at bit funky at first glance. This is the same as doing this:
// Step 1: using the rest parameter to collect the arguments
function omelette(...args) {
args; // ['๐ฅ', '๐ง']
// Step 2: extract the value using destructuring
const [egg, cheese] = args;
}
I did the above in 2 steps, but I could also combine all the steps into one:
// "..." --> reflects the rest parameter
// "[egg, cheese]" --> reflects the destructuring
function omelette(...[egg, cheese]) {
egg; // '๐ฅ'
cheese; // '๐ง'
}
Resources
- MDN Web Docs: Array Destructuring
- ES6: Destructuring โ extracting data from arrays and objects in JavaScript
- GitHub Gist: Destructuring
- Destructuring Assignment in ES6
- MDN Web Docs: arguments object
- Stack Overflow: What's the difference between an argument and a parameter?
- Stack Overflow: Arguments or Parameters
- Originally published atย www.samanthaming.com
Thanks for reading โค
Say Hello! Instagram | Twitter | SamanthaMing.com
Top comments (10)
Nice read!
Really well explained, I tend to use objects for this purpose, so the user sees the parameter names on the function call, but this is a great approach as well, even better if using
arguments
.Thanks Manolo! And glad you found it helpful!
Totally, passing object is super cool! which kinda acts like named parameters where you don't have to worry about the order. I believe this is what you're referring to:
I have a tidbit on it as well > samanthaming.com/tidbits/18-named-... ๐
Yup that was exactly it :) I'll check it out
For 2 arguments it is kinda not needed, although when passing in a bunch of arguments it is cleaner to do it this way.
If I have to pass a lot of parameters I tend to use with objects instead:
I have to agree with you in this sense, I guess the point of the post was to not use
arg[0]
to access arguments and to show and explain how destructuring works. But in the example you show, it is kinda redundant. I think this will confuse more people than not.This is why I use objects instead, at least you give some useful feature to the user (named parameters).
This:
fn({ name: 'bob' })
.Instead of:
fn('bob')
.Thanks for the question Tomasz! I just re-wrote the article to add some explanation. Hopefully, it clarified some things...if you don't mind, can you have a read (the last section) and let me know if it helps ๐คIf it doesn't, just let me know and I'll try again ๐
For sure, in this simple example, the rest+destructuring combo doesn't really make sense. I'd stick with the 2nd way for sure ๐I think the community example was just showing a different way of doing things that achieve the same thing. Coding is all about communication, so we should pick the one that best facilitates that. I always enjoy seeing all the different ways because it builds up my toolbox. The best tool is always dependent on the situation. Thanks for pointing this out and allowing me to adjust my code notes ๐
Great technique!, Ad thanks the tip about default args @lukeshiru
Learned something new! Thank you :)
Awesome, glad to hear that! thanks for reading ๐