0

I have created REST API using node.js.since the API consumer is not correctly populating the object & some of the object attributes are coming "undefined" and sometimes undefined.

I have added a request interceptor that rejects API request if the required request parameters are missing

this is how my code looks like today. I just want to check if there is any better way to handle it.

(!variable_name || variable_name === 'undefined') 
14
  • 1
    Why would you set a variable to 'undefined' instead of undefined Commented Aug 31, 2018 at 0:00
  • 2
    can you be more specific, !variable means it could be 0, empty string, undefined, null (and I'm probably forgetting others). you can check for exact equality to undefined by saying foo === undefined. note the three equals. the string 'undefined' is just a word, you may as well have said 'bar' there. Commented Aug 31, 2018 at 0:00
  • 2
    also you probably mean || instead of !! Commented Aug 31, 2018 at 0:01
  • 1
    I am doing like and getting a syntax error in the console no doubt Commented Aug 31, 2018 at 0:06
  • 1
    @I.R.R. you only need if your variable might not exist. Like testing for window in an unknown context. If you know you have a variable available, typeof is not necessary. Commented Aug 31, 2018 at 1:00

1 Answer 1

2

The most specific and self-explaining way is:

foo === undefined || foo === 'undefined'

While

!foo || foo === 'undefined'

condition is equivalent to

foo === undefined || foo === null || foo === false ||  foo === '' ||  foo === 0 || foo === NaN || foo === 'undefined'

This may result in false positive for any listed falsy value. If this is the case, !foo shouldn't be used.

A shorter way is to coerce foo to a string:

'' + foo === 'undefined'

This may result in false positive for any object that has toString() returning undefined. If this is undesirable, it shouldn't be used.

This is a workaround to fix a problem that shouldn't exist in the first place. undefined shouldn't be indistinguishable from 'undefined' string because this way there's no way to tell if it was originally 'undefined' string ('undefined' is a word) or undefined that was accidentally stringified.

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

6 Comments

The most self-explaining way is to avoid this situation altogether and treat "undefined" as it is => a string.
@Kaiido I'm not sure what you mean. OP clearly stated that a value can be either undefined or 'undefined' string because it was supplied by API this way. I agree that the distinction between those two just complicates everything at this point.
And that's this API that needs to be fixed. Adding chewing-gum everywhere to avoid leaks will just make your fingers sticky.
@Kaiido Also, depending on the meaning of this value, 'undefined' string may be totally unacceptable because it could be originally a string with this value and there's no way to distinguish it from 'undefined' string that was erroneously created.
Indeed. That just goes my way. (ps: I think your answer is good at answering this bad question, but a small note about you're doing it wrong™ might be a bonus.
|

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.