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') 
Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
Vini
  • 29
  • 7
  • Your title says _"&"_ but your question says _"or"_; which is it? – Phil Aug 31 '18 at 00:00
  • 1
    Why would you set a variable to `'undefined'` instead of `undefined` – MinusFour Aug 31 '18 at 00: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. – aaron Aug 31 '18 at 00:00
  • Your JavaScript isn't a valid expression – Phil Aug 31 '18 at 00:01
  • 2
    also you probably mean `||` instead of `!!` – aaron Aug 31 '18 at 00:01
  • 1
    `I am doing like` and getting a syntax error in the console no doubt – Jaromanda X Aug 31 '18 at 00:06
  • sorry for the typo, its ||. – Vini Aug 31 '18 at 00:09
  • I have created REST API in using node.js. I can see for some reason the variable is coming 'undefined' & for some cases undefined. – Vini Aug 31 '18 at 00:13
  • I will prefer !foo insted of foo === undefined. wondering if (!foo || foo === 'undefined') can be shorten as well – Vini Aug 31 '18 at 00:14
  • Possible duplicate of [How to check for an undefined or null variable in JavaScript?](https://stackoverflow.com/questions/2559318/how-to-check-for-an-undefined-or-null-variable-in-javascript) – Ian Aug 31 '18 at 00:21
  • I've only ever got this to work: `if (typeof variable_name === "undefined")`. – Iskandar Reza Aug 31 '18 at 00:21
  • 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. – Mike 'Pomax' Kamermans Aug 31 '18 at 01:00
  • Oh that explains a lot, since most of the time I'm working with data that may or may not be there. – Iskandar Reza Aug 31 '18 at 01:02
  • 1
    "*the API consumer is not correctly ..."* Fix this then. – Kaiido Aug 31 '18 at 01:45

1 Answers1

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.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565
  • The most self-explaining way is to avoid this situation altogether and treat `"undefined"` as it is => a string. – Kaiido Aug 31 '18 at 01:44
  • @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. – Estus Flask Aug 31 '18 at 01:49
  • And that's this API that needs to be fixed. Adding chewing-gum everywhere to avoid leaks will just make your fingers sticky. – Kaiido Aug 31 '18 at 01:50
  • @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. – Estus Flask Aug 31 '18 at 01:53
  • 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. – Kaiido Aug 31 '18 at 01:54
  • @Kaiido I can easily imagine that the OP has no other choice with faulty API. But I added a clarification for future readers. – Estus Flask Aug 31 '18 at 02:07