Loading…
Everything will be ready in few seconds
- Theory
- Theory
- Comments
Order of parameters
Let’s do something else other than calculating miles for a little while. There’s one nuance about using arguments: they must be transferred in the same order in which the parameters of the function are declared.
Let’s look at this using an example. We have the getFavoriteBook
function, which logs a message about the user’s favorite book in the console. It has two parameters: userName
(user name) and bookName
(book title).
var getFavoriteBook = function (userName, bookName) {
console.log('My name is ' + userName + '. My favorite book: ' + bookName);
}
Suppose our user’s name is Simon, and his favorite book is “Hedgehog in the fog”. Let’s transfer these arguments to the function:
var getFavoriteBook = function (userName, bookName) {
// Parameter userName keeps 'Hedgehog in the fog'
// Parameter bookName keeps 'Simon'
…
}
// Call the function
getFavoriteBook('Hedgehog in the fog', 'Simon');
// Console will log: My name is Hedgehog in the fog. My favorite book: Simon
Something seems to have gone wrong. Why did we get this result? After all we have transferred user name and book title, everything is the way it was supposed to be.
The issue has to do with the order of the arguments. Parameters work as variables: a value from an argument is written to a parameter, and is then used inside a function by name. In our case, everything happened exactly like that. The first parameter of the function is userName
, the first argument is 'Hedgehog in the fog'
. This argument is also written into the parameter userName
, and the argument 'Simon'
became the parameter bookName
. Of course you know where’s the name and where’s the book title, but JavaScript doesn’t. It understands everything literally: what was transferred first became the first parameter. Because the order of the arguments corresponds to the order of the parameters in the function. We have the parameters written in this order: userName, bookName
. Hence, the user name must be transferred first and the book title afterwards.
var getFavoriteBook = function (userName, bookName) {
// Parameter userName keeps 'Simon'
// Parameter bookName keeps 'Hedgehog in the fog'
…
}
// Call the function
getFavoriteBook('Simon', 'Hedgehog in the fog');
// Displays: My name is Simon. My favorite book: Hedgehog in the fog
As you can see, we transferred the arguments in the right order and the message is now correct.
By the way, if you do not transfer a parameter at all, its value will be undefined
that is, nothing. Here the result can also be unexpected. Be careful.
Let’s practice calling a function with a different order of arguments.
By the way, now that we are done working with lines, you will see that the situation with numbers is even more fun. If the function has parameters of different types, for example, numbers and lines, and the arguments are transferred in the wrong order, unexpected results may occur. For example, concatenation may take place and the number will be added to a line, the result of which will be a line. Or you could get value NaN
, which stands for “not a number”. It means that it is impossible to calculate the result of a mathematical operation.
- script.js
Thanks! We’ll fix everything at once!
The code has changed, click “Run” or turn autorun on.
Comments