Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
Default params in JavaScript

I want to create a function with default params, but I want to use one default param and then use a custom value for the following one. For instance,

function greet(name, greeting = 'Hey', question = 'How are you?') {
    console.log(`${greeting}, ${name}! ${question}`);
  }

greet('Jack', , 'You good?'); // tried this, doesn't work :(

What if I wanted to say, "Hey, Jack! You good?", where I use the default for the first param with a default value, but not the second? I have tried calling the function with nothing between the commas in the function call. Is there a way to do this in JS without explicitly passing 'Hey', the defined default value, to the param that has a default already assigned?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

function greet(name, greeting = 'Hey', question = 'How are you?') {
    console.log(`${greeting}, ${name}! ${question}`);
  }

greet('Jack',undefined , 'You good?');

about 4 years ago · Juan Pablo Isaza Report

0

You can pass undefined for the second param. It should work as you expect it to be.

greet('Jack', undefined, 'You good?');
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!