I'm coming to Javascript from Python. In Python if you use a list or dictionary as a default argument for a function, every call sees the same object. So if you have a function like:
def append_to_list(lst=[]):
lst.append(1)
return lst
and then call it like:
lst1 = append_to_list()
lst2 = append_to_list()
lst2 will have value [1, 1] instead of just [1]
Does Javascript have the same issue with default arguments?
It does not seem to have the same issue. Testing with function:
function append_to_list(lst=[]) {
lst.push(1)
return lst
}
And calling the same way returns [1] both times.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With