MZ Selfswitch 'true' or true vs. 'false' or false

I'm trying to learn about creating plugins right now. I came across something that confused me a bit.
I have a function that changes multiple selfswitches at once.

Spoiler: Code
PluginManager.registerCommand(pluginName, "SetSelfSwitch", args => {
var sswitch = JSON.parse(args.selfswitches).map(e => JSON.parse(e));
sswitch.forEach(e => {
if (e.state === 'true') {
e.state = true;
}
if (e.state === 'false') {
e.state = false;
}
SetSelfSwitch(e.mapid, e.eventid, e.letter, e.state);
});
});

This works how I want it for now.
But if I try to set state as a string and not as a boolean, it only works with true (i.e. 'true' instead of true).
Why does it work on true but not on false ('false' instead of false)?
JavaScript's type coercion rules treat all non-empty strings, including "false", as truthy:
A typical conversion from "true"|"false" to boolean is:
JavaScript:
let result = value === 'true';
I.e. define result equal to true iff value is the string "true", otherwise false.
A small addition to caethyril's post.

The Plugin Manager always outputs values as String, regardless its type. This is why we (devs) always have to convert it to the actual type that we need.

I don't know about the "setSelfSwitch" code, but I think, that you also need to convert the other values.

Code:
SetSelfSwitch(
  Number(e.mapId),
  Number(e.eventId),
  e.letter,
  'true' === e.state
);
@caethyril
It could just be because I pulled an all-nighter, and my brain is mush right now... but type coercion shouldn't really matter in this situation, should it? It would apply to something like if ("false"). But if (e.state === "false") will only ever evaluate to true if e.state contains that exact string.

But more importantly, I'm confused about why the OP even has those if statements. JSON.parse should have already turned them into proper boolean variables by that point, no?
thank you very much for your answers.
JavaScript's type coercion rules treat all non-empty strings, including "false", as truthy:
I should have known that. I'm not completely new to programming (but I've never had anything to do with js before) but sometimes I'm just a bit stupid. 🙂

I don't know about the "setSelfSwitch" code, but I think, that you also need to convert the other values.
I'm used to declaring variables with appropriate types and am somewhat unfamiliar with passing arguments in this way (haven't used it in years - mainly c# + wpf). And even if it works at this point without the conversion, I will definitely have to remember that for the future and convert all variables accordingly so that I don't get problems elsewhere
You're welcome!

... But if (e.state === "false") will only ever evaluate to true if e.state contains that exact string.
That's their working code, though. The question was about the difference between e.state = "false" and e.state = false. 👋
JSON.parse should have already turned them into proper boolean variables by that point, no?
It only parses one level down.
  • The first parse clearly expects an array, since Array#map is called on the result.
  • The second parse converts each element from string -> object.
The result is an array of objects, where each object has string keys and string values.

It's often a good idea to only use JSON.parse for string -> object conversion (including arrays) to avoid ambiguity, e.g. "000" vs 0 or "true" vs true. There are type-specific ways to convert to any given primitive type (string, number, boolean, etc).
You're welcome!


That's their working code, though. The question was about the difference between e.state = "false" and e.state = false. 👋

It only parses one level down.
  • The first parse clearly expects an array, since Array#map is called on the result.
  • The second parse converts each element from string -> object.
The result is an array of objects, where each object has string keys and string values.

It's often a good idea to only use JSON.parse for string -> object conversion (including arrays) to avoid ambiguity, e.g. "000" vs 0 or "true" vs true. There are type-specific ways to convert to any given primitive type (string, number, boolean, etc).

Now that I've got some caffeine in me, both of those things make perfect sense. Thanks for clearing it up.

Original thread: https://forums.rpgmakerweb.com/threads/selfswitch-true-or-true-vs-false-or-false.148058/