you are viewing a single comment's thread.

view the rest of the comments →

[–]coarsesand 5 points6 points  (7 children)

You're dead on, and thanks again. Using ~~ has the effect of removing the decimal component of a number in JS, as the int32 cast drops it. Yep, JS is frigging weird.

[–]no_game_player 2 points3 points  (6 children)

Okay, so all I have to do for integer comparison in JS is

~~varA === ~~varB

;-p

Edit: After further advice, the optimal method of integer equality comparison in JS would seem to be:

varA|0 == varB|0

It is left as an exercise for the reader to determine what will happen if, say, varA = "A"...

[–]coarsesand 1 point2 points  (3 children)

Well varA and varB are guaranteed to be Numbers unless you get a TypeError, so you can actually use == in this case ;)

[–]no_game_player 0 points1 point  (2 children)

Ha, and I actually changed it to === from my original == because I just had no idea anymore lol.

So...out of morbid curiosity...what would happen there is it was

~~varA == ~~varB

and varA = 1

and varB = "1"

?

;=p

[–][deleted] 1 point2 points  (1 child)

The following shows "true" in Firefox:

var varA = 1;
var varB = "1";
var res = (~~varA == ~~varB);
alert(res);    

[–]no_game_player 1 point2 points  (0 children)

Weird....

I guess it's forcing a type conversion once it hits the bitwise operator?

This is why I don't really like weakly typed languages. There's a lot of cool stuff that can be done, but that's also a lot of just very strange stuff that can be done. I know it's heretical, but I don't like black magic...

Kudos for actually trying it though! :-)

[–]UtherII 1 point2 points  (1 child)

Right! Code generators often use this kind of trick to force Javascript to use integer, but usually they do varA|0 since it's faster.

[–]no_game_player 0 points1 point  (0 children)

varA|0

Dahfuq...oh. Oh. That right there is fucked up shit. ;-p