mjshi

Jack of Most Trades
Veteran
Joined
Feb 16, 2013
Messages
987
Reaction score
826
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
865
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
865
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

I added footstep sounds to our content that I posted in a previous thread. I was unable to edit the post, so if you're interested, see below.

DOWNLOAD LINK

I've started working on rpg maker again, and I can see a lot of improvement from when I was 13. I still have a long way to go, but I'm proud that I've gotten this far.
Animated Title screen is by SharkerrBlue(SB)
I am using the Nightmare Land pack.
I just can't walk to the store for lunch without walking past people smoking. It not only smells bad, it's probably poisoning me. I'm afraid I'll eventually have to be hospitalized. I can't take another path because there's too much traffic and drivers in the plaza are reckless sometimes. I almost got run over once, even after looking both ways.
RyanYe wrote on whtdragon's profile.
Your art works about Wyrvens and Dragons are sooooooooo cooooooooool~! I like them so bad
Can you create more? like Archangels, Devils, Titans, Cyclops!!
My valentine cutscene is now available on itch.io and you can get the "source code" as well, feel free to check it out!

The game page on itch can be found at:

Forum statistics

Threads
108,795
Messages
1,039,549
Members
141,245
Latest member
feedtalefour
Top