Click here to Skip to main content
14,242,360 members

JavaScript Functions - Implicit Parameters

Rate this:
2.81 (5 votes)
Please Sign up or sign in to vote.
2.81 (5 votes)
26 Dec 2017CPOL
Javascript Function's implicit parameters explained

Before explaining the implicit parameters, it would be appropriate to discuss a little about JavaScript function's parameters and arguments. Most of the time, we use these two terms interchangeably. But they've a subtle difference:

  • a parameter is the variable that is listed as part of the function definition
  • an argument is the value that is passed against the parameter defined in the function when the function is invoked

When the function is invoked, the arguments are assigned to the parameters of the function in the specified order. Meaning, the first argument is assigned to the first parameter and second argument is assigned to the second parameter and so on. In JavaScript, if the list of arguments is greater or lesser than the number of parameters, it doesn't complain about it, which leads us to the new ES6 feature rest parameter.

Rest Parameter

If for some reason, you're not sure how many arguments you will end up passing with invoking a function, you can always use rest parameter with ellipsis (...) prefixed to the parameter name. The parameters passed as rest parameter are available in the function in the form of array. For example:

function myFunc(firstParam, ...remainingParams) {
     console.log(firstParam);
     for(var i = 0; i < remainingParams.length; i++) { 
          console.log(remainingParams[i]);
     }
}
myFunc('A','B','V','F','T','H');

The above function will log the first parameter to the console followed by the number of parameters passed as rest parameter that are available in remainingParams array to be logged into the console.

Now let's move towards the implicit parameters of JavaScript functions.

Whenever a function is invoked, besides the list of obvious parameters, a couple of hidden parameters are also passed into that function, i.e., argument, and this.

Implicit Parameter - argument

The argument parameter is a collection of all the arguments passed to a function. The argument parameter is useful in case no parameter is defined or a matching parameter is not defined, then this could be used to access the values passed to the function. To identify the number of arguments passed, we could use the arguments length property as shown below:

function myFunc() {
     if(arguments.length > 0) {
          for(var i = 0; i < arguments.length; i++)
                console.log(arguments[i]);
     }
}
myFunc(2,3,4,6,1,7,3);

Implicit Parameter - this

When a function is invoked, with the explicitly defined parameters that represents the argument, an implicit parameter this is also passed. The this parameter refers to the object that invoked the function. This is why this parameter is also known as function context.

function myFunc() { 
     return this;
}
myFunc();

The above function when called in global context would return the window object. If we try to execute this in strict mode, then it would return undefined, like:

function myFunc() {
     'use strict'
     return this;
}
myFunc();

The this parameter is a tricky fellow. Unlike other high level object oriented languages where this always points to the object of the class, this parameter is highly influenced by the method of invocation.

This brings us to the different ways of invoking a function which will be explained later in the next article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

muhammadjawwad
Pakistan Pakistan
No Biography provided

Comments and Discussions

 
QuestionOff-by-one error Pin
andylepa@tlen.pl22-Dec-17 9:16
memberandylepa@tlen.pl22-Dec-17 9:16 
QuestionRe: Off-by-one error Pin
JustWatchLittle 26-Dec-17 1:40
professionalJustWatchLittle 26-Dec-17 1:40 
AnswerRe: Off-by-one error Pin
andylepa@tlen.pl26-Dec-17 2:06
memberandylepa@tlen.pl26-Dec-17 2:06 
GeneralRe: Off-by-one error Pin
JustWatchLittle 26-Dec-17 2:07
professionalJustWatchLittle 26-Dec-17 2:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Tip/Trick
Posted 20 Dec 2017

Tagged as

Stats

8.2K views
5 bookmarked