News for hackers. Stuff that matters.
Re:JavaScript Is Weird by kdmccormick on Sunday August 13, 2023 @01:02PM (#37111775)
Sure, I'll try. Imagine we had two functions already defined:

  get_current_username: Returns a nonempty string iff user is logged in.

  get_balance: Returns a user's "balance" of some sort of credit or currency. Negative balances are possible.
In Python, where "falsiness" is very clearly defined by the language spec, it would be idomatic to write:

  should_render_balance = bool(get_current_username() and get_balance())
Re:JavaScript Is Weird by positron6000 on Sunday August 13, 2023 @02:34PM (#37112735)
so you get the exact same answer?

in js, Boolean('' && 0) will give you the same answer as bool('' and 0) in python.

if i code golf in python (like the article does in js), i'll also get weird-looking code and "unexpected" outputs.

Re:JavaScript Is Weird by kdmccormick on Sunday August 13, 2023 @03:23PM (#37113331)
Sure, I was just responding to the question:

> Like, can you tell me a language where it does make sense to "apply AND to an empty string and a (negative?) zero"?

People do stuff like that in Python all the time. Personally, I don't do it in JS, though, because I find the conversion-to-bool rules much less predictable. In this case, though, I guess JS would behave the same.