简体   繁体   中英

What is adder in this code snippet? I can't find an answer

I'm trying to figure out what is a const variable if we assign it to a array.push method

const numbers = [0,33,4,1]
const adder = numbers.push(2)

What does adder become here ? If i write it in the console it gives me the length of the numbers array. Is it bad practice to assign an array method to a variable ?

What does adder become here ?

It's assigned the return value of push . So look at the documentation for push :

The push() method adds one or more elements to the end of an array and returns the new length of the array.

So it is the length of the array.


Is it bad practice to assign an array method to a variable ?

In general (there are exceptions) it isn't useful to assign an array method to a variable … but that isn't what this code is doing. The return value of a method is not the method itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM