Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override JavaScript default parameter with undefined

I have a function with argument bar that has a default parameter, "". How do I override bar's default parameter with value undefined?

const foo = (bar = "") => {   console.log(bar) }  foo(null) // null foo(undefined) // "" <-- I want this to log `undefined` 

If this is impossible with default parameters, what would be an appropriate way to write foo to achieve this?

like image 367
jchi2241 Avatar asked May 06 '20 02:05

jchi2241


People also ask

How will you override the default parameter value?

Bookmark this question. Show activity on this post. In the following code, call to Method2 receives the Value parameter as False, even though base class does not declare default value for the parameter at all, and derived class declares True as default.

Can I pass undefined to a function JavaScript?

See, undefined is passed as argument, but not passed as a parameter when we invoke the function. So, inside the function the value of the variable undefined is (guaranteed) the original value of undefined .

Can you assign the default values to a function parameters True False?

Yes, it is correct that you can assign a default value to the parameter before calling the function, which results in you not needing to pass an argument when calling the function. The default parameter is however always overridden if you pass a value to the function.

Can you assign the default values to a function parameters?

Default parameter in JavascriptThe default parameter is a way to set default values for function parameters a value is no passed in (ie. it is undefined ). In a function, Ii a parameter is not provided, then its value becomes undefined . In this case, the default value that we specify is applied by the compiler.


2 Answers

what would be an appropriate way to write foo to achieve this?

If you mean to default only when there is no parameter passed to the function call, then you need to check the arguments length, or to spread the arguments if you want to keep an arrow function.

const foo = (...args) => {    const bar = args.length ? args[0] : "";    console.log(bar)  }    foo(null) // null  foo(undefined) // undefined  foo(); // ""
like image 161
Kaiido Avatar answered Sep 24 '22 20:09

Kaiido


No, you can't, by design.

You've run into an interesting demonstration of JavaScript's 'two nulls', null and undefined.

null is a designated null value

undefined is the absence of any value at all

You ask about the passing the 'value' undefined but that premise is flawed. There is no value undefined - undefined is the lack of a value.

Therefore, you shouldn't pass undefined as a meaningful value to be interpreted by a function. I mean, you can, but from the point of view of JavaScript it is equivalent to passing nothing at all - so you're fighting against the design of the language and will run into issues like this one.

If you want to pass a meaningful, purposeful null value, that is what null is for.

like image 24
davnicwil Avatar answered Sep 22 '22 20:09

davnicwil