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=3and thenfoo(2, 3), so variablesxandyin 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 callfooin the same way regardless of swapping ofxandy.