ES6: Set Default Parameters for Your Functions?

Hello all…this may sound like a silly question, so forgive me, but I wanted to reverse the Arrow Function in the example code for the Arrow Function - [I can workout the actual task but can’t solve *how it might* have looked before]

const greeting = (name = "Anonymous") => "Hello " + name;

console.log(greeting("John")); // Hello John
console.log(greeting()); // Hello Anonymous

I would like to know what this looked like in the normal sense without/pre ‘Arrow function’

This is what I thought:

const greeting = function(name) {
  "use strict";
  if(name == "") {
    return "Anonymous";
  }   
  return "Hello " + name;
};
console.log(greeting("John")); //Hello John
console.log(greeting()); //Hello Anonymous

However it nearly works but returns this:

Hello John
Hello undefined

Thanks for any advice.

Challenge: Set Default Parameters for Your Functions

Link to the challenge:

Remember two things

  1. undefined and "" are not the same. You aren’t passing in an empty string when you have no parameters.
  2. You exit the function upon a return. If your condition is fixed, then you will return only "Anonymous".
1 Like

Your arrow function has no condition, why are you adding a condition to your regular function?

How would you translate the following regular function to an arrow function:

const sum = (a,b) => a + b;
1 Like

Thanks for the reply…
So I never realised we could write ‘undefined’. I very new to learning JavaScript!

So I revised this but think it maybe a bit clunky -

const greeting = function(name) {
  "use strict";
  if(name == undefined) {
    return "Hello " + "Anonymous";
  }   
  return "Hello " + name;
};
console.log(greeting("John")); //Hello John
console.log(greeting()); //Hello Anonymous

Is this what you mean?

Yep. You could also do

if (!name) {
  ...
}

but the results are slightly different, as you will get "Hello Anonymous" for any falsey value, to include empty strings or 0.

1 Like

Hi…thanks for your reply, I’m not too sure what you mean unless you are referring to the if statement having " "…instead of saying name == undefined? [as mentioned in the previous reply I didn’t realise you could use ‘undefined’ as a condition].

The second part I presume would be:

const sum = function(a, b) {
  "use strict";
  return a + b;
};

I really appreciate your help…and sorry if I’m a bit slow on the uptake. I really am only a couple months into this and trying to cram it all in…so maybe missing certain elements.

M

Ok…wow…I feel really good after this now. Like I said I’ve been journeying with JavaScript [programming in general] for about a month - and i’ve been trying to get as much as possible into my head. It’s a steep learning curve and quite lonely learning on your own. But replies like yours really go along way for the confidence. Thank you my friend.

I’m happy to help. The curiosity to ask questions like “How do I try to add default parameters for non Arrow Functions?” will definitely help you on your journey : )

1 Like

I asked you how to translate an arrow function to a regular one because It seemed to me you were misunderstanding default parameters, which is what the lesson is all about. If I wanted to translate the greeting arrow function to a regular one, I don’t need to have a condition in my regular function, because there isn’t one in your arrow function to begin with.

Although your greeting function using the function keyword is valid syntax, when one says regular function what’s usually expected is something like this:

function sum (a,b) {
  return a + b:
}

And it’s a good thing that you take the time to ask questions like that by the way.

1 Like

Ah right I get you…thanks. Makes sense. My problem being at this beginners level is i’m taking things very literally. I’m finding it a bit fuzzy trying to be lateral.

Like seeing your answer makes perfect sense…and I’m saying to myself ‘why didn’t I see as simply ‘function sum’ when writing it down’. It will come with time. Thanks for taking the time to help me though. Appreciate it.
M