Parameters vs arguments
A quick terminology note: parameters are the variables in the function definition. Arguments are the actual values you pass when calling the function.Python mental model: JavaScript is more permissive than Python here. If you pass too few arguments, the missing ones become
undefined (instead of raising a TypeError). If you pass extra arguments, JavaScript ignores them unless you collect them with a rest parameter (...args).Default parameters
Give parameters a fallback value when no argument is provided:- JavaScript
- Python
=, Python uses =. No surprises here.
Practical example
Rest parameters
Collect any number of arguments into an array using...:
...numbers gathers all arguments into a real array. You can then use array methods on it.
- JavaScript
- Python
...args, Python uses *args. Same idea.
In JavaScript,
... is used in multiple places (rest parameters, spread syntax). In Python, *args and **kwargs are split into separate syntaxes/uses. Same family of idea, different syntax rules.Destructuring parameters
When a function takes an object, you can destructure it right in the parameter list:Destructuring with defaults
Combine destructuring and default values:What’s next?
You know how to define functions and pass data into them. Next, let’s understand where variables live — scope determines which parts of your code can see which variables.Understanding scope
How JavaScript determines where variables are accessible