A JavaScript Show
Function Parameters and ArgumentsEarlier in this tutorial, you learned that functions can have parameters: function functionName(parameter1, parameter2, parameter3) { Function parameters are the names listed in the function definition. Function arguments are the real values passed to (and received by) the function. Parameter RulesJavaScript function definitions do not specify data types for parameters. JavaScript functions do not perform type checking on the passed arguments. JavaScript functions do not check the number of arguments received. Default ParametersIf a function is called with missing arguments
(less than declared), the missing values are set to Sometimes this is acceptable, but sometimes it is better to assign a default value to the parameter: Example function myFunction(x, y) { Try it Yourself » The Arguments ObjectJavaScript functions have a built-in object called the arguments object. The argument object contains an array of the arguments used when the function was called (invoked). This way you can simply use a function to find (for instance) the highest value in a list of numbers: Examplex = findMax(1, 123, 500, 115, 44, 88); function findMax() { Try it Yourself » Or create a function to sum all input values: Examplex = sumAll(1, 123, 500, 115, 44, 88); function sumAll() { Try it Yourself » If a function is called with too many arguments (more than declared), these arguments can be reached using the arguments object. Arguments are Passed by ValueThe parameters, in a function call, are the function's arguments. JavaScript arguments are passed by value: The function only gets to know the values, not the argument's locations. If a function changes an argument's value, it does not change the parameter's original value. Changes to arguments are not visible (reflected) outside the function. Objects are Passed by ReferenceIn JavaScript, object references are values. Because of this, objects will behave like they are passed by reference: If a function changes an object property, it changes the original value. Changes to object properties are visible (reflected) outside the function. Example: Function as Parameter
Output Hello John Hello Jack Hello Sara In the above program, there are two functions:
Functions are data, and therefore can be passed around just like other values. This means a function can be passed to another function as an argument. This allows the function being called to use the function argument to carry out its action. This turns out to be extremely useful. Examples best illustrate this technique, so let's look at a couple now. 11.3.1. Example: setTimeout¶The built-in function setTimeout(func, delayInMilliseconds); Example Suppose we want to log a message with a 5 second delay. Since five seconds is 5000 milliseconds (1 second = 1000 milliseconds), we can do so like this:
repl.it Console Output Try It! Is the call to The function A common twist often used by JavaScript programmers is to use an anonymous function as an argument. Example This program has the same behavior as the one above. Instead of creating a named function and passing it to
Examples like this look odd at first. However, they become easier to read over time. Additionally, code that passes anonymous functions is ubiquitous in JavaScript. 11.3.2. Example: The Array Method map¶The array method let mappedArray = someArray.map(functionName); When the
When complete, Example
Console Output [3.14, 42, 4811] [ 6.28, 84, 9622 ] Notice that When using Example This program has the same output as the one immediately above. The mapping function is defined anonymously within the call to
Console Output 11.3.3. Check Your Understanding¶Question Similar to the
repl.it Question Use the
repl.it Can I use a function as an argument in JavaScript?Passing a function as an argument to the function is quite similar to the passing variable as an argument to the function. so variables can be returned from a function.
Can a function take a function as an argument?Because functions are objects we can pass them as arguments to other functions. Functions that can accept other functions as arguments are also called higher-order functions.
Can a function call be an argument?Calling the function involves specifying the function name, followed by the function call operator and any data values the function expects to receive. These values are the arguments for the parameters defined for the function. This process is called passing arguments to the function.
Can you use a function in a function JavaScript?Nested functions
A function is called “nested” when it is created inside another function. It is easily possible to do this with JavaScript. Here the nested function getFullName() is made for convenience. It can access the outer variables and so can return the full name.
|