0 added
0 removed
Original
2026-01-01
Modified
2026-03-09
1
<p>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.</p>
1
<p>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.</p>
2
<p>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).</p>
2
<p>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).</p>
3
var getFavoriteBook = function (<b>userName</b>,<b>bookName</b>) { console.log('My name is ' +<b>userName</b>+ '. My favorite book: ' +<b>bookName</b>); }<p>Suppose our user’s name is Simon, and his favorite book is “Hedgehog in the fog”. Let’s transfer these arguments to the function:</p>
3
var getFavoriteBook = function (<b>userName</b>,<b>bookName</b>) { console.log('My name is ' +<b>userName</b>+ '. My favorite book: ' +<b>bookName</b>); }<p>Suppose our user’s name is Simon, and his favorite book is “Hedgehog in the fog”. Let’s transfer these arguments to the function:</p>
4
var getFavoriteBook = function (<b>userName</b>,<b>bookName</b>) { // Parameter<b>userName</b>keeps 'Hedgehog in the fog' // Parameter<b>bookName</b>keeps 'Simon' … } // Call the function getFavoriteBook('Hedgehog in the fog', 'Simon'); // Console will log: My name is<b>Hedgehog in the fog</b>. My favorite book:<b>Simon</b><p>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.</p>
4
var getFavoriteBook = function (<b>userName</b>,<b>bookName</b>) { // Parameter<b>userName</b>keeps 'Hedgehog in the fog' // Parameter<b>bookName</b>keeps 'Simon' … } // Call the function getFavoriteBook('Hedgehog in the fog', 'Simon'); // Console will log: My name is<b>Hedgehog in the fog</b>. My favorite book:<b>Simon</b><p>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.</p>
5
<p>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.</p>
5
<p>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.</p>
6
var getFavoriteBook = function (<b>userName</b>,<b>bookName</b>) { // Parameter<b>userName</b>keeps 'Simon' // Parameter<b>bookName</b>keeps 'Hedgehog in the fog' … } // Call the function getFavoriteBook('Simon', 'Hedgehog in the fog'); // Displays: My name is<b>Simon</b>. My favorite book:<b>Hedgehog in the fog</b><p>As you can see, we transferred the arguments in the right order and the message is now correct.</p>
6
var getFavoriteBook = function (<b>userName</b>,<b>bookName</b>) { // Parameter<b>userName</b>keeps 'Simon' // Parameter<b>bookName</b>keeps 'Hedgehog in the fog' … } // Call the function getFavoriteBook('Simon', 'Hedgehog in the fog'); // Displays: My name is<b>Simon</b>. My favorite book:<b>Hedgehog in the fog</b><p>As you can see, we transferred the arguments in the right order and the message is now correct.</p>
7
<p>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.</p>
7
<p>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.</p>
8
<p>Let’s practice calling a function with a different order of arguments.</p>
8
<p>Let’s practice calling a function with a different order of arguments.</p>
9
<p>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.</p>
9
<p>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.</p>