-1 evaluates to true?

mjshi

Jack of Most Trades
Veteran
Joined
Feb 16, 2013
Messages
980
Reaction score
822
First Language
English
Primarily Uses
N/A
Because the integer -1 is not equal to the Boolean true.


But if you're asking "if this number exists", then the if statement would only evaluate false if the number was 0.
 

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
863
First Language
German
Primarily Uses
Because it's a truthy value. Everything, that's not


- 0


- ''


- null


- undefined


- NaN


- false


will evaluate to true inside an if.


/edit: You can test it, by typing this into the console:


!!-1


The first "!" will negate the -1, converting it into a boolean. The second negates it again, so you can effectively get the boolean representation of any object.
 
Last edited by a moderator:

SilverDash

Veteran
Veteran
Joined
Oct 11, 2015
Messages
426
Reaction score
173
First Language
Dutch
Primarily Uses
RMMV
I see... the if-statement treats the -1 differently because it handles types different compared to both the === and == operators.


That's why the tilde-operator is such a nice way of handling someArray.indexOf() within an if-statement. Because it converts the -1 to 0, which as a 'truthy' evaluates to false.
 
Last edited by a moderator:

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
863
First Language
German
Primarily Uses
~ is a bitwise operator, as you can see here: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators


It never occured to me to use it for indexOf, but if it works, that's nice. I usually use a bitwise OR as a faster alternative to Math.floor:


1.324324|0 // 1


Well, it's not exactly floor, as it will just cut of everything after the dot, but if you're only dealing with positive number it does the job and has the nice side effect of converting non-numbers into 0:

Code:
[1, 2, 3]|0		// 0
 

SilverDash

Veteran
Veteran
Joined
Oct 11, 2015
Messages
426
Reaction score
173
First Language
Dutch
Primarily Uses
RMMV
Yes it works very nicely :D


This is how I code some of my indexOf checks:


if(~[1,2,3].indexOf(3)){console.log(true);}


JSHint gives me a warning but it works.
 
Last edited by a moderator:

Latest Threads

Latest Posts

Latest Profile Posts

I remember once when I was at Disney's Animal Kingdom and I got to dance with some of the cast members! It was so much fun! After the show my dad and I got frozen lemonade and some ice cream. It makes me feel better remembering the good things about my dad... maybe I will return to Orlando.
spending almost entire day to mod skyrim but it still couldn't run properly :(
My friend has become one with the glitter. @ImaginaryVillain, got any room in the Cult of the Sporkle?
I still cannot wrap my head around how to edit and use arrays in RPG Maker MV o_O
Gamemaking has been slower, but I do my best to make small progress every day. :)

Forum statistics

Threads
107,716
Messages
1,031,625
Members
139,856
Latest member
ShadowGray
Top