Invoke Function with Default Value for First Argument and Custom Value for Second Argument in JavaScript

In JavaScript, functions can be defined with default parameters. This means that if no argument is provided for a parameter when the function is called, the default value will be used instead.

Consider the following function:

function greet(name = "World", greeting = "Hello") {
  console.log(`${greeting}, ${name}!`);
}

In this function, name defaults to "World" and greeting defaults to "Hello". If we call greet() with no arguments, it will output "Hello, World!".

Now, what if we want to use the default value for name but provide a new value for greeting?

One way to achieve this is to pass undefined for the first argument. This will cause JavaScript to use the default value for that argument.

function greet(name = "World", greeting = "Hello") {
  console.log(`${greeting}, ${name}!`);
}

greet(undefined, "Hi"); // "Hi, World!"

However, passing undefined as an argument can be misleading as it might imply that the argument is intentionally being set to undefined.

A more elegant solution would be to use an options object as the function parameter. This allows us to pass in an object with only the properties we want to set, and it will use the default values for the rest.

Here's how we can modify the greet function to use an options object:

function greet({ name = "World", greeting = "Hello" } = {}) {
  console.log(`${greeting}, ${name}!`);
}

Now, we can call greet with a new value for greeting and it will use the default value for name:

greet({ greeting: "Hi" }); // Outputs: "Hi, World!"

This approach is more explicit and less prone to errors, as it's clear which parameters are being set and which are using their default values.

Learn Higher-Order Functions

Write code that is easier to understand and maintain.

You've heard about higher-order functions, but every explanation you've come across is either too technical or too vague. You're searching for something clear and practical, with examples that will help you practice and truly master the concept.

Higher-order functions are key to writing better code, essential for your projects, and expected in interviews. But getting started can be tough.

But what if you could? What if you had access to easy-to-understand content that not only explains higher-order functions but also provides hands-on practice? You’d finally feel confident using them in your projects, knowing exactly what they do and how to leverage them effectively.

It’s true, finding the right resources to learn higher-order functions can be challenging… but it doesn’t have to be.

Get my guide, where you’ll:

  • Understand why higher-order functions are worth learning and how they can transform your approach to writing JavaScript.
  • Gain clarity as I break down higher-order functions into simple, everyday language.
  • Master three important higher-order functions: map, filter, and reduce, with plenty of examples to solidify your understanding.
  • Apply your knowledge with real-world examples that demonstrate how these concepts work together, so you can use them easily in your projects or write better code in your interviews.
  • Get practice exercises to help reinforce what you’ve learned and boost your confidence in understanding the concepts.

Get the Guide