-2

I was working on some backend node.js if condition when i realized i wasnt getting an error when i do this if (value.accepters !== undefined && value.accepters.length !== 0) while checking an object with a missing key but if i do value.accepters.length i get error, can anyone explain why for better understanding, thanks.

An example: a bit messy, please don't mind. enter image description here

Code Update

var value = {accepters: ["id1"], rejecters: [], blockedRoom: []}
// did some long code work here
value = {accepters: ["id1"], rejecters: []}
console.log("blockedRoom check 1:", value.blockedRoom !== undefined && value.blockedRoom.length !== 0) // No Error
console.log("blockedRoom check 2:", value.blockedRoom.length !== 0) // Error

var accepters = 0;
var rejecters = 0;
var amongAccepters = false;
var amongRejecters = false;
var amongBlockers = false;
// No Error
if (value.accepters !== undefined && value.accepters.length !== 0){
  accepters = value.accepters.length;
  const foundUserAmongAccepters = value.accepters.find(
    (v) => v.key === key
  );
  if (foundUserAmongAccepters !== undefined) {
    amongAccepters = true;
  }
}
if (value.rejecters !== undefined && value.rejecters.length !== 0) {
  rejecters = value.rejecters.length;
  const foundUserAmongRejecters = value.rejecters.find(
    (v) => v.key === key
  );
  if (foundUserAmongRejecters !== undefined) {
    amongRejecters = true;
  }
}
if (value.blockedRoom !== undefined && value.blockedRoom.length !== 0) {
  const foundUserAmongBlockers = value.blockedRoom.find((v) => v === key);
  if (foundUserAmongBlockers !== undefined) {
    amongBlockers = true;
  }
}
Abdullah Isa
  • 29
  • 1
  • 6
  • 1
    Please post code, data, and results as text, not screenshots. http://idownvotedbecau.se/imageofcode – Barmar Sep 06 '22 at 19:31
  • Since you don't get "hmm" or "hmmmmm" I'd assume that l.m is undefined. – James Sep 06 '22 at 19:32
  • 1
    `&&` performs short-circuiting. So when you write `expression1 && expression2`, `expression2` is not evaluated when `expression1` is false. So you don't get an error in the second expression if the first one prevents it. – Barmar Sep 06 '22 at 19:33
  • What's unclear about the error message? If you try to read a property of `undefined` you get an error. Only trying to read the property of the value after you have confirmed if it is not `undefined` avoids that problem. – Quentin Sep 06 '22 at 19:37
  • something *being* undefined doesn't cause an error. accessing a property on something that doesn't exist *does* cause an error. – Kevin B Sep 06 '22 at 19:44
  • @Barmar i added code but i think i get it now, in an if statement the first check while doing `if (value.blockedRoom!== undefined && value.blockedRoom.length !== 0)` failed because `blockedRoom` is not in the object and that means it won't check if it has any length even though it has `&&` operator, correct me if i am wrong, thanks. – Abdullah Isa Sep 06 '22 at 21:08
  • @Barmar just saw ur explanation, clear things up will consider this solved, thanks. – Abdullah Isa Sep 06 '22 at 21:10
  • That's correct. You can also use the ES6 optional chaining operator syntax: `value?.blockedRoom?.length !== 0` – Barmar Sep 06 '22 at 21:10
  • @Barmar nicee, will test and make use of this `value?.blockedRoom?.length !== 0` thanks. – Abdullah Isa Sep 06 '22 at 21:12
  • @Barmar can help write up the solution? thanks. – Abdullah Isa Sep 06 '22 at 21:25

2 Answers2

0

You set var l = { k: 1 }

And then you try to log m.length, that doesn't exist.

You then check if l.m is not undefined, and log. It's undefined, so you don't log.

You then do the same check, is l.m? It's not, so we should circuit the if check and we don't log.

Next, try to log l.m.length, we know l.m is undefined, so l.m.length is undefined. Error

Next you try to do an if check on something we already know fails, because l.m is undefined, l.m.length fails (undefined.length == error), so the === 0 never happens.

Same as last comment.

Finally, check if l.m is defined, it's not, short circuit to false.

Tom
  • 7,640
  • 1
  • 23
  • 47
  • Thanks i think i understand now, `Finally, check if l.m is defined, it's not, short circuit to false` i was wondering why it's not checking for the length after it knows that `m` is not in the object, thanks. – Abdullah Isa Sep 06 '22 at 20:54
-1

Solution and better understandings from @Barmar

&& performs short-circuiting. So when you write expression1 && expression2, expression2 is not evaluated when expression1 is false. So you don't get an error in the second expression if the first one prevents it.

Abdullah Isa
  • 29
  • 1
  • 6