Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

You can't replace a complex concept with a boolean. This isn't JavaScript.


sort by: page size:

Js doesn't really care about types, and doesn't seem to convert a string "true" into a boolean true:

    > temp0.innerHTML = true // no error
    > "true" == true
    false
    > true == "true"
    false

Does JavaScript not have something like "bool(value)"?

That's just negation in JS.

`0 === false` is maybe the only thing that I actually hate about javascript. Mutability, I understand and thus can deal with it. Scope, no biggie. But resolving a number to false, when all other numbers resolve to true? Inexplicable.

EDIT: `!0`


Saw the title. Immediately thought of something I saw today at work. Not too different from yours. 'if ('True' == 'True')'. I should note that this is javascript generated by a template file.

That only works if it's surrounded by parenthesies. A bare object literal is not syntactically valid JavaScript.

Yeah, is one of the things I don't like about JS. The typeof operator should be able to handle those cases. Or a new operator that returns a boolean would be nice.

    if(defined jQuery.unknown.propierties)

> You cannot override that in Javascript

True. But you they can use a "known Symbol" to implement this feature.


> Javascript natively is quite difficult to do this and requires a lot of code to handle simple logic.

Statements like this are what boggle my mind. I just don't see what's so damn difficult about native Javascript.


I had to read that paragraph several times to see if they said "But Javascript doesn't do this." Nope, not in there at all.

Javascript's concatenation and equality rules alone likely will lead to your first premise being incredibly false.

Javascript lets you do it if you use the equality (==) instead of the identity (===).. i think.

Really? What is hard about “u=rw;g=r;o=“ etc - literally no calculations involved.

This “reinvent things that already work fine but in javascript” is getting old.


Can you do it in js without using conditionals.

You can't do that in idiomatic JavaScript, at least I've never done such a thing in the past 15 years. Maybe you could share an example to show me?

I’m not sure a switch statement against a string is a great solution either although to some extent you are restrained by JavaScripts limitations.

Why is everyone claiming this is impossible in JavaScript? If you have a POC you should post it so others can learn of the danger.

You've even been quoted elsewhere in this thread about this topic.


It's even more puzzling since those quotes are not required in JavaScript...

I don't think it is useful to know the behaviour of JS in such cases. In practice, you rarely compare strings with numbers, or objects with booleans.

I wrote tens of thousands of lines of JS (e.g. this library https://github.com/photopea/UPNG.js ), I never used "===" in my life :D

next

Legal | privacy