Reputation: 182
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.
Upvotes: 2
Views: 738
Reputation: 32146
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
Note: the arguments
variable is only accessible on non-arrow functions!
Upvotes: 3