mjshi

Jack of Most Trades
Veteran
Joined
Feb 16, 2013
Messages
987
Reaction score
827
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
866
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
427
Reaction score
174
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
866
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
427
Reaction score
174
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 Profile Posts

Interesting week. Lots of stress and anxiety especially on Tuesday but still got thru it. How is everyone doing?
I was pleased with how the little ... whatever those square bits like upside-down turrets on the skirt are? I like how they turned out. Here's a little ranger/bandit/archery enthusiast to model it:

TestChara-08.gif


I'm calling her Robin because obviously.
Just downloaded the demo and boy howdy are my arms tired (???) :kaohi:
I just watched a trailer for the upcoming 2021 JRPG Fantasian. They demonstrated an interesting mechanic where random encounters can be sent to a stockpile instead of fighting them right away, which can be emptied in a single battle string at the player's choice of timing.
"The pleasure from the senses seems like nectar at first, but it is sour as the toxin in the end." -Kirshna

Forum statistics

Threads
109,156
Messages
1,042,592
Members
141,656
Latest member
drackdragon113
Top