2
if( !body || body[0] === '<') {...}

I am using the above statement to check if a variable body is undefined or null or anything but the value that is expected, but I keep getting a TypeError:

TypeError: Cannot read property '0' of undefined

How can this be? According to the very popular answer on checking for undefined the first part !body of the above statement should return true if body is anything from null, undefined, NaN, "", 0, or false - and therefore not evaluate the right-hand side of the OR condition: body[0] === '<' when that is the case.

Is there a standard function to check for null, undefined, or blank variables in JavaScript?

What else could I check for to make sure that body[0] is only called when body is not undefined? I also already tried if(typeof body === 'undefined' || body[0] === '<') with the same described behaviour. Any thoughts on this will be highly appreciated. Thanks you.

(Just for further information: body is the response of an REST-API request, if everything goes smooth it returns a string that can be parsed into a JSON-object. However, in some cases an entire HTML-file (containing error information) is returned hence I'm checking for '<' at the beginning of the response. Sometimes the variable seems to be 'undefined' which throws the error when checking body[0].)

oystersauce
  • 631
  • 1
  • 6
  • 17
  • 4
    Are you sure the error is thrown in that line, and not one line down inside the `if` where you try to access `body[0]` again? – deceze Mar 13 '18 at 16:19
  • I think something else is going on. [Here's a fiddle](https://jsfiddle.net/y5qpk0av/2/) showing your code working fine under several conditions – James Mar 13 '18 at 16:31
  • @deceze is on to something, `!body` should shortcut in `if (!body || ..` – rtn Mar 14 '18 at 02:18

3 Answers3

1

I think you have the logic backwards.

function isFirstCharLt(value) {
  return !!value && typeof value === 'string' && value[0] === '<';
}

console.log(isFirstCharLt(undefined));
console.log(isFirstCharLt(null));
console.log(isFirstCharLt('<string'));
console.log(isFirstCharLt('string'));
Amos47
  • 695
  • 6
  • 15
1

You're not showing us all the codez ;)

The code block will be run if body is undefined since it shortcuts || in the if statement and I suspect you're checking body[0] somewhere in that code block.

if (!body || body[0] === '<') {
  // is body[0] used here? 
}

It's better to make sure body has the correct properties, e.g.

if (typeof body === 'string' && body[0] === '<') {}
rtn
  • 127,556
  • 20
  • 111
  • 121
0

i would use two if statements, check if its defined first then validate:

if(body) {
  if (body[0] !== '<') {
    'DO-SOMETHING'
  }
}
Kyle Joeckel
  • 469
  • 2
  • 9
  • 1
    You can condense that to `if (body && body[0] ...)`, and then you basically have what OP already has. – deceze Mar 13 '18 at 16:33
  • but its not what he already has, what he has isnt working – Kyle Joeckel Mar 13 '18 at 16:36
  • 1
    But it *should*, that's the issue. First understand why it *isn't* working. – deceze Mar 13 '18 at 16:37
  • because its undefined which is why i am validating that high level then validating the content of the body. there is definitely a more intricate way of doing this but this would work – Kyle Joeckel Mar 13 '18 at 16:41