== vs ===

SilverDash

Veteran
Veteran
Joined
Oct 11, 2015
Messages
424
Reaction score
171
First Language
Dutch
Primarily Uses
RMMV
I know the difference between the two of 'em. But because Javascript is not a strict language and because other plugins may make mistakes (like putting '2' in the Actor.actorId instead of 2), using === may not always be a good idea unless it's purely for comparing your own variables?

I usually try to use as much === as possible, but only for my own variables. Is that good or bad? :p .

Strings are a special case:

Of course for comparing objects. However, using === for strings can be dangerous if a string was created with String:

alert(new String('foo') === 'foo'); // falsealert(new String('foo') == 'foo'); // trueBecause you never know if the other person in some other plugin for some reason used a String() instead of a '' you can't use === for comparing strings that come from the RM-API either.
 
Last edited by a moderator:

jamespedid

Veteran
Veteran
Joined
Jun 10, 2013
Messages
30
Reaction score
3
First Language
English
Strict equality is typically preferred because of its strictness of type. However, if you're running into issues of equality where you're comparing two completely different types, there's a chance that your coding needs improvement. Also, never use new String(). There's literally no point to doing so.
 

SilverDash

Veteran
Veteran
Joined
Oct 11, 2015
Messages
424
Reaction score
171
First Language
Dutch
Primarily Uses
RMMV
However, if you're running into issues of equality where you're comparing two completely different types, there's a chance that your coding needs improvement
Not in my code... code from the RM-API that may have been messed up by other plugin writers who are not so strict with types. Some people accidentally story a string-value in the Actor.actorId and if I would perform a === comparison with it, my plugin would bug even though my plugin is the one doing it correctly :/.

Also, never use new String(). There's literally no point to doing so.
I don't. But again, I can't 100% trust other people to do the same. What if some popular plugin-writer uses String() and puts the value back into the RM-API? It could cause bugs in my scripts which can be really hard to track down.

The === operator is on average ~10% faster (and I read it can be up to 1000% faster in some cases!) than the == operator in terms of performance. But this greatly differs per browser.
 
Last edited by a moderator:

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
863
First Language
German
Primarily Uses
If other plugins store wrongly formatted values, that's entirely their fault.
 

SilverDash

Veteran
Veteran
Joined
Oct 11, 2015
Messages
424
Reaction score
171
First Language
Dutch
Primarily Uses
RMMV
Today I saw another scripter who does exactly that... Stores floats as strings. Also used "strict mode" in the global scope and of course an anonymous function so I can't extend his script w/o fully overwriting it. And on top of that cluttering the global with (unused!) global variables (so much for the anonymous function...) and much more...

When looking through other people's code (which is good practice if you want to use it for your project, you don't want plugins written by amateur-scripters because they will just raise bugs in other scripts), I often see the same 'mistakes'.

I could just blindly use the === operator on RM-API's. BUT, if I use other people's scripts (not written by pro's like Yanfly, Iavra or Viktor, etc.) I risk a lot of nasty bugs that can be really bothersome to track down. Until I probably decide to disable the other plugins and find out that some 'amateur scripter' used the wrong type and put it back into the RM-API..

Basically, the === operator when used on the RM-API can potentially be bothersome. But it can also potentially increase performance by a lot. Especially in big loops in the update-method. Therefor I thought it was a good idea to only use the === on your own variables, and use the == on the RM-API.
 
Last edited by a moderator:

jamespedid

Veteran
Veteran
Joined
Jun 10, 2013
Messages
30
Reaction score
3
First Language
English
You can obviously adapt to the ineptness of other people's codes, or opt not to use them and roll your own.
 

Reavenator

Member
Veteran
Joined
Jun 8, 2013
Messages
142
Reaction score
48
Primarily Uses
Maybe you could just open any scripts your using, and type "new string" in the search function, and then replace the command?

It sounds tedious, but it's a possibility.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,862
Messages
1,017,045
Members
137,569
Latest member
Shtelsky
Top