Function parameters and arguments
Functions in JavaScript can receive information to work with through parameters and arguments. Parameters are placeholders for values that the function expects to receive, while arguments are the actual values that are passed into the function when it's called.
For example, imagine a function that calculates the area of a rectangle. It needs to know the width and height of the rectangle to be able to perform its calculations. In this case, the width and height are the parameters. When the function is called, the actual values for the width and height are provided as arguments.
Here's an example of a function that takes two parameters, width
and height
, and prints the area of a rectangle:
In this case, width
and height
are the parameters, and they are used to calculate the area of the rectangle. When the function is called with actual values for width
and height
, these values are used as arguments:
In this example, 5
and 10
are the arguments passed to the function. The function will use these values as width
and height
, and will print 50
to the console.
Functions with parameters and arguments allow for more flexibility and reusability in code, as the same function can be used with different values depending on the situation.
Code Challenge!
Write a function named double
that takes a single argument and prints the value of that argument multiplied by 2 using console.log
.
For example, if you call the function with double(5)
, it should print 10
to the console.