0

My function has a parameter dtoIn and I am trying to check if it's undefined or null.

I have values assigned like this so it shouldn't be undefined, but I get that the object is undefined anyway. Can I have some advise please.

{
    count : 10,
    minAge : 10,
    maxAge : 10,
}

function main(dtoIn={count, minAge, maxAge}) { 
  if (typeof dtoIn !== "undefined" && dtoIn !== null){
    alert(`empty`);
  }
}
3
  • That isn't a valid default value unless you're defining count, minAge and maxAge somewhere in the global scope. Also by defining a default value (if you do it correctly) dtoIn will never be undefined or null. Commented Jun 11, 2022 at 11:17
  • You need assign your object to a variable, right now you are creating an object and discarding it. Commented Jun 11, 2022 at 11:22
  • Even so, the default value in the function will throw errors. To make it valid you need to provide a valid default object function main(dtoIn={count:10, minAge:10, maxAge:10}) {... Commented Jun 11, 2022 at 11:23

2 Answers 2

2

I think there are a couple of misunderstandings in your code as to how object destructuring and function default values work.

Consider the following example:

let obj = {
    count : 10,
    minAge : 10,
    maxAge : 10,
}

function main({ count = 15, minAge = 15, maxAge = 15 } = {}) {
    console.log(count, minAge, maxAge)
}

main(obj);
main();

We don't actually even need to perform any undefined checks here, because this syntax is for either destructuring a given object to count, minAge and maxAge OR if nothing is given, we destructure into count = 15, minAge = 15 and maxAge = 15.

If you want to keep the dtoIn named parameter and have default values for it, you can do that like so:

let obj = {
    count : 10,
    minAge : 10,
    maxAge : 10,
}

function main(dtoIn = { count: 15, minAge: 15, maxAge: 15 }) {
    console.log(dtoIn);
}

main(obj);
main();

Sign up to request clarification or add additional context in comments.

6 Comments

That's assuming their goal was to create separate variables in the function and not create a dtoIn object with keys/values from other variables...really hard to tell.
@vanowm Yeah. My approach here is to provide at least some kind of sensible solution, even if it isn't exactly what the asker wanted. Often times we have more or less confusing questions and people asking how to do something that is not even possible, because they have understood something wrong in the first place. So the only sensible thing to do is to offer some kind of "best guess" answer. :)
The only sensible thing is to vote to close for clarification. Providing a default object and destructuring are not necessarily interchangeable, though in this basic example they may be, plus you've now possibly compounded the OP's misunderstanding of defaults by combining it with destructuring and having to provide an empty default object in order to trigger the defaults in the destructuring.
@pilchard That's what the code of conduct tells us anyway. I've never really liked how sensitive we are to closing not-so-perfect questions, when there are often great opportunities to learn. We shouldn't gatekeep asking questions if you aren't able to perfectly describe the problem and can't offer an unambiguous example, be it because of slight problems regarding language barrier or technical understanding. I'd get your point if I hadn't provided links to documentation and explained what I did.
Then at least offer the most basic solution which is just to provide a valid object as default since there is no indication at all that the OP was attempting to destructure. If anything it strikes me as a misunderstanding of object property shorthand
|
-1

I guess you're assigning function params wrong try this way

let dtoIn = {
count : 10,
minAge : 10,
maxAge : 10,
};

function main(val) { 
  if (typeof(val) == "undefined" && val !== null){
    alert("empty");
  }
}

main(dtoIn);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.