Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
@C0D4 nah, I don't think this would work... Because 2 != true (nor does 3)
π€π€π€
Or am I wrong? -
C0D4681467y@kolaente
Shove it in the consoleπ
2 !== true
But 2 == true
As 2 is not false.
If you were using === for comparison, then no you are right it won’t be true.
however you are not so JS will use falsely comparisons until it can compare. -
plusgut60377y@kolaente you either define a get trap or use the toString method and increment it, each time it's called.
-
Cant you just
var i=0
function a(){
return i++
}
Or something along these lines? -
koin5887yconst a = {
i: 1,
toString: function () {
return a.i++;
}
}
if(a == 1 && a == 2 && a == 3) {
console.log('Hello World!');
} -
Saw this question trending on SO the other day. Apparently was a shitty, gotcha-style interview question.
-
Using "==" instead of "===" in javascript is bad practice. Any code lint will give you an error. It's not "gotcha" interview question if you ever did any real coding besides onclick events in js.
-
plusgut60377y@kargaroth this task is obviously nothing you would do in the real world. It's to figure out if somebody has deep knowledge about the core mechanics of the language.
-
plusgut60377yYes and no. Trying to cast an object to a number, is obviously a bad idea. I agree that the dynamic typing is broken.
But having the possibility to use the core mechanics, like displaying an object as a string with toString, or having the possibility to define get/set/whatever traps, is a very powerful and great tool.
Can (a ==1 && a== 2 && a==3) ever evaluate to true in JavaScript?
rant
javascript you never fail to amaze me