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

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


sort by: page size:

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

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 can't replace a complex concept with a boolean. This isn't 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


That's just negation in JS.

javascript could be really nice if they fixed these:

0 is falsey


Javascript lets you do anything wrong if you use == and not === though, I'm pretty sure the machine apocalypse is going to happen because someone types == instead of === at this point.

`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`


Usually it's awful. I wish JavaScript had if else expressions. Sometimes it can help to break up the lines with indents if you need a single expression:

  var myvar = foo < bar
    ? bar
    : bar < baz
      ? baz
      : 0

FWIW, I almost always use ==. Very rarely, if ever, had any problems- probably I'm used to javascript enough to think in advance of the edge cases and use === when I feel it's necessary.

In the above case, Javascript is coercing the strings to numbers.

=== does no type coercion. You can also coerce types manually. JS is not statically typed, but it is typed.


No, JS is not weird, your code is weird.

true + ("true" - 0) ?

"" && -0 ?

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

If you write code like this, you have much bigger issues than whatever you choose to write your code in.


A lot of javascript causes errors if you try to do stuff on non-existent elements:

> document.getElementById("nonexistent").innerHTML = "";

This however will not cause any errors, it just won't do anything:

> $("#nonexistent").html("");

These sort of do-nothing-but-don't-throw-an-error responses can encourage developers to be less careful about making sure elements exist as they are expected, etc.


I guess in javascript, that's true.

It is trivially easy to avoid though, just use === instead of ==.

Most of the JavaScript wtf's are people getting confused by the implicit type conversions (like [] == ![]). If you ask for it, JavaScript will readily convert a string to a number or an object to a boolean. But if you don't like that, just don't rely on implicit conversions.


It's so funny. Anybody still using == with js?

Unproductive, how?

Also, this isn't a javascript problem; this is a problem of trying to be too clever and not understanding implicit conversion, which happens in many languages.


JS gets even weirder when you include all the comparison operators.

https://jsfiddle.net/5Yzs6/17/

Did you know undefined is equal to undefined, but not <= undefined or >= undefined?

Or that two instances of the same object/array are not equal (e.g. [-1] != [-1]), but are both <= and >= each other?

Or that /.*/ is non-comparable (only != is true) to all numbers, but greater than [-0.5] and "-0.5", and less than [0] and "0"?


That one isn't specific to Javascript, though. If you can find a mainstream language that doesn't have any values that aren't equal to themselves, I'll give you a cookie.

(Going from memory of the last time I tried this challenge, since I can't get it to load at the moment.)

next

Legal | privacy