Functions are defined in Python and JavaScript in a similar way, but with subtle differences.
Python | Javascript |
---|---|
Named parameters # | |
x = 2 y = 3 x = 2 y = 3 x = 3 y = 2 |
x = 2 y = 3 x = 2 y = 3 x = 2 y = 3 3 2
x = 2 y = 3 |
You may be tempted to use a Pythonic syntax on named function arguments in Javascript. Unfortunately, it won't work. Moreover, JS doesn't support named function arguments at all. Line
foo(x=2, y=3)
in fact executes three commands:x=2
,y=3
and thenfoo(2, 3)
, so variablesx
andy
in the calling scope will be created/updated. Assignment operator returns the value, so the function will be called asfoo(2, 3)
. The next line will callfoo
in the same way regardless of swapping ofx
andy
.