HTML Academy
I have a parameter for you
Functions7/24
Back to the list of tasks
  • 1. Fasten your seat belts
  • 2. Long-distance flight
  • 3. Going around again
  • 4. Helper function
  • 5. Function, I’m calling you!
  • 6. I have a parameter for you
  • 7. Order of parameters
  • 8. Return from function
  • 9. Summary of “Functions”. Part 1
  • 10. Seventh program: “From salary to salary”
  • 11. Business trip
  • 12. Write, simplify
  • 13. Remove the unnecessary code
  • 14. Make me beautiful
  • 15. Just add percentage
  • 16. Call me quietly by name
  • 17. I see the target
  • 18. How many flights?
  • 19. Let’s clean up a little
  • 20. Who is faster?
  • 21. Saving up for a trip around the world
  • 22. Summary of “Functions”. Part 2
  • 23. Eighth program: “Money makes money”
  • 24. Ninth program: “The Eternal Question”
Return from function
  • Sign up
  • Log in

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 the name is and where the book title is, 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 pass an argument at all, the corresponding parameter has the value undefined. 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 strings, you will see that the situation with numbers is even more fun. If the function has parameters of different types, for example, numbers and strings, and the arguments are passed in the wrong order, unexpected results may occur. For example, concatenation may take place and the number will be concatenated with a string, producing a string. 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.

Comments

  • script.js
JavaScript
var showGreeting = function (name, age) { console.log('Hello! My name is ' + name + '. I’m ' + age + ' years old.'); };

What didn’t you like in this task?

Thanks! We’ll fix everything at once!

Console

The code has changed, click “Run” or turn autorun on.

Result

Goalscompleted

Call the showGreeting function using different ways. Write each call on a new line.

  1. Call the function without arguments.
  2. Call the function with argument 5.
  3. Call the function with arguments 5 and 'Muffin'.
  4. Call the function with arguments 'Muffin' and 5.

Cookies ∙ Privacy ∙ License Agreement ∙ About ∙ Contacts ∙ © HTML Academy OÜ, 2019−2026

VISAMastercard

Log in

or

Forgot your password?

Sign up

Sign up

or
Log in

Restore access

Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.

Forgot to connect your email to the profile? Email us and we’ll help.