简体   繁体   中英

JavaScript - Is passing undefined as a parameter the same as leaving it out?

Pretty straightforward question. For example, are these equivalent?

window.open('http://someurl.com')

window.open('http://someurl.com', undefined)

Part of me suspects that it might depend on the function itself, but I'm not sure.

In short: In the vast majority of cases, passing undefined is equivalent to leaving the argument out.

However, within a function, you can differentiate between an omitted argument and one that was passed as undefined . While I would never recommend using that distinction to change behavior, you can see the difference between the two if you access the special arguments variable within your function:

 function logNumArgs() { console.log(arguments.length) } logNumArgs() // 0 logNumArgs(undefined, undefined) // 2 

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