0

I used logical assignments like b = a || [] in Javascript before, and there are many questions on SO about it.
To mention some:

And from what I read, b = a || [] is legit code.

However, I noticed in a recent case (checking for a variable set by another script), that this code does not work as I expected.
I assumed the code would check whether a is falsy, and if so [] would be assigned to b. Since undefined is falsy, I excepted the code to work and assign [] in the case when a was not defined.

But this code causes an error:

let b;

b = a || [];
console.log(b);

a = ["1", "2", "3"];
b = a || [];
console.log(b);

wheras

let b;

b = typeof a == "undefined" ? [] : b;
console.log(b);

a = ["1", "2", "3"];
b = a || [];
console.log(b);

does not. And why is it causing an error, when the error says Uncaught ReferenceError: a is not defined, which implies a is undefined?

When can I use the logical assignment? Only when it is guaranteed that the variable (a) is at least declared?

jost21
  • 1,084
  • 3
  • 15
  • 29
  • 1
    Reading an undeclared variable is always an error. It's not falsy. I'd suggest you never work with variables that are undeclared. – VLAZ Apr 01 '22 at 17:02
  • Well, sometimes it's not up to you, that's why I check for it. – jost21 Apr 01 '22 at 17:05
  • 1
    But why does then `b = typeof a == "undefined" ? [] : b;` not cause an error? – jost21 Apr 01 '22 at 17:05
  • 1
    Since JavaScript now has null coalescence operator `??`, in practice it's much safer than using `||` for a "fallback" behavior since we'll have to read a type conversion chart to ensure the behavior is what we expect. – Daniel Cheung Apr 01 '22 at 17:05
  • 1
    No, it's *always* up to you. There is no real option to have a "possibly undeclared" unless you're sloppy or just don't want to write proper code. – VLAZ Apr 01 '22 at 17:06
  • @DanielCheung reading an undeclared variable still causes an error. – VLAZ Apr 01 '22 at 17:06
  • @VLAZ You are right. Though I was going to mention my comment wasn't relating to the understanding part of the problem but just the title of the question. – Daniel Cheung Apr 01 '22 at 17:08
  • [Why does accessing a non-existent object property result in `undefined` instead of throwing a `ReferenceError`?](https://stackoverflow.com/q/31901994) | [var vs this in Javascript object](https://stackoverflow.com/q/4946625) | [What is the purpose of the var keyword and when should I use it (or omit it)?](https://stackoverflow.com/q/1470488) | [How to check undefined in Typescript](https://stackoverflow.com/q/43716263) | [How to check if a variable is undefined versus it is undeclared in javascript?](https://stackoverflow.com/q/64406377) – VLAZ Apr 01 '22 at 17:09
  • @VLAZ I don't think any of those explain why `b = typeof a == "undefined" ? [] : b` does not cause an error? – jost21 Apr 01 '22 at 17:12
  • 2
    [typeof !== "undefined" vs. != null](https://stackoverflow.com/q/2703102) | [variable === undefined vs. typeof variable === "undefined"](https://stackoverflow.com/q/4725603) | [How can I check for "undefined" in JavaScript?](https://stackoverflow.com/q/3390396) | [How to check a not-defined variable in JavaScript](https://stackoverflow.com/q/858181) – VLAZ Apr 01 '22 at 17:13
  • Thank you, this is basically the answer I was looking for https://stackoverflow.com/a/2703122/1786528 – jost21 Apr 01 '22 at 17:15
  • Even so I agree with you in principle that it should be avoid, I still disagree that it is *always* up to you. When you are not the only developer or/and there is an existing code base you cannot change, you sometimes need to make sure a variable is declared/defined. That is what I was looking for. – jost21 Apr 01 '22 at 17:26

0 Answers0