Free JavaScript course. Sign Up for tracking progress →

JavaScript: Optional function parameters

In programming, many functions and methods have parameters that rarely change. In such cases, these parameters are given default values that can be changed as needed. This reduces the number of duplicates in your code. An example:

// Exponentiation function.
// The second parameter is 2 by default
const pow = (x, base = 2) => {
  return x ** base;
};
// 3 to the second power (two is set by default)
pow(3); // 9
// three to the third power
pow(3, 3); // 27

The default value looks like a normal assignment within the definition. It only works if the parameter is not passed. This is something you should get used to. There can even be a default value when there is only one parameter:

const print = (text = 'nothing') => console.log(text);

print(); // "nothing"
print("Hexlet"); // "Hexlet"

There can be any number of default parameters:

const f = (a = 5, b = 10, c = 100) => { ... }

The default values have one limitation. They should be at the very end of the parameter list. From the point of view of syntax, it's impossible to create a function that will have a mandatory parameter after an optional one:

// Such code will terminate with an error
const f = (a = 5, b = 10, c = 100, x) => { ... }
// As well as this one
const f = (a = 5, x, b = 10, c = 100) => { ... }

// But this one will work
const f = (x, a = 5, b = 10, c = 100) => { ... }

// This one will work too
const f = (x, y, a = 5, b = 10, c = 100) => { ... }

Instructions

Write the getHiddenCard() function that takes a credit card number (consisting of 16 digits) as a string and returns its hidden version, which can be used on the website to display. If the original card had the number 2034399002125581, the hidden version will look like ****5581. In other words, the function replaces the first 12 digits with asterisks. The number of asterisks is specified by a second optional parameter. Its default value is 4

// The credit card number is passed as a string
getHiddenCard("1234567812345678", 2); // "**5678"
getHiddenCard("1234567812345678", 3); // "***5678"
getHiddenCard('1234567812345678');    // "****5678"
getHiddenCard('2034399002121100', 1); // "*1100"

To complete the task, you'll need the repeat() string method, which repeats a string a given number of times.

"+".repeat(5); // "+++++"
"o".repeat(3); // "ooo"
The exercise doesn't pass checking. What to do? 😶

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
In my environment the code works, but not here 🤨

Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.

My code is different from the teacher's one 🤔

It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.

In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.

I've read the lessons but nothing is clear 🙄

It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.

By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.


If you got stuck and don't know what to do, you can ask a question in our community