Search⌘ K
AI Features

Exercise on Default Arguments

Explore how to implement default arguments in JavaScript ES6 through practical exercises, including running callbacks after delays and managing optional parameters effectively. Understand how to simplify function coding and handle cases without all arguments.

Exercise 1:

Write a function that executes a callback function after a given delay in milliseconds. The default value of delay is one second.

The setTimeout() method can be used to specify the time delay before a function is executed.

Exercise 1
Solution
function executeCallback( callback, delay ) {
console.log('Delay: ' + delay);
}
//Edit above this line
executeCallback( () => console.log('Done'));

Explanation:

The main objective of this exercise was to define a default argument for delay.

Using ES6 conventions, we can simply state the default value in the function arguments:

delay = 1000

The statement above sets the delay at 1000. You ...