As one of the most popular programming languages in the world, JavaScript is widely used for building dynamic and interactive websites, web applica...
For further actions, you may consider blocking this person and/or reporting abuse
Good stuff. Another common gotcha:
This is because
var
creates a single binding at the function scope, so after a one-second timeout the loop has already run its course, hence you get3
three times. By usinglet
you bind the variable at the block scope (loop), so it returns the values you expected sincei
refers to the value at that loop iteration.If someone wants to overcome this while using
var
, not many people know thatsetTimeout
can accept params to be passed to the callback, great alternative to bind.Good tip. I used this pattern a lot before ES6.
Good fix
I gave your suggestion a space, here it's..
Thank you again.. Happy reading!!!
this is insightful.
Yes
Yes perfect example. I will add it to the article..
Personally, I donβt use βvarβ. I always use βletβ and βconstβ to avoid these kinds of bugs..
Thank you for your valuable feedback
This was really informative and solved one of most annoying glitch of my small project. Thanks
You're welcome my friend. Glad you found it helpful and thank you for your feedback..
The moral of this article is
1) don't try to be clever. Just write clean, clear expressive code.
2) Using Typescript will avoid the majority of the accidental type coercion errors.
3) please don't rely on these coercion oddities for logic gates. Be explicit.
I appreciate your input on this article
Interesting... But most of this begins to make more sense if you understand how loose typing works through coercion and the seven falsy values.
I learned this I've 25 years ago.
It also helps when using JS to process potentially unreliable input. JS is also pretty nice for ETL work.
One thing I've found cool is that ~~ double bitwise not will coerce any value into a number in the 32-bit signed integer range. Not number or easily interpreted string values will be 0.
Yes it's more important to understand the loosely type languages..
Thank you sir for your valuable feedback..
5οΈβ£ is terribly wrong and your explanation just contradicts to itself:
Is "0" even non-empty?
Does
true == "2"
?Does
if ("0") { console.log("hi"); }
execute?Read the doc: developer.mozilla.org/en-US/docs/W...
I will update the explanation. Thanks
I've always loved these little tricks (although they do lead to some nasty mistakes sometimes lol) and this is a wonderful compilation! I also really appreciate that you made the NaN example say baNaNa
Glad you loved it and thank you for appreciating it. Happy reading..
Itβs kind of frightening to see that the language that runs all of the web and a lot of backend applications has so many quirks.
Yes it should be refactored. Thanks you for the feedback..
Nice write up!
People generally love JavaScript for its duck-typing, but this article illustrates that there are quite a few traps that we can fall in to if we aren't careful.
Thank you my friend. Yes I agree with you.. we should be careful with JavaScript. I appreciate your feedback..
Π‘ongratulations π₯³! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up π
Thank you my dear friend. I appreciate it
These results are only 'unexpected' if you don't fully understand the language and how to use it. They can also be used to your advantage.
Yes youβre right. But sometimes it happens, even for experienced developers..
Thank you for your feedback
This is really helpful cleared core concept which is mainly we are using day to day coding.
Thank you for appreciating it
Thank you, Parth. Tamaro feedback mne gamyo..
ππ
Well done @codeofrelevancy This is awesome
Thank you sir for appreciating it..
Accurate compilation of JS 'craziness' that could drive a human insane lol
Yes it could..
Thanks for comment
This is crazy
Thanks for sharing this
Youβre welcome my friend. Thank you for your kind message
Good One π
Thank you boss
This article is a great advert for the use of TypeScript. Hardly any of the examples would even compile in TypeScript, as they shouldn't in any language that takes type safety seriously
Yes agree, TypeScript is better in this case. Thank you sir for your valuable feedback..
Wow,what an informative and educative Article. Thanks for sharing
You're welcome dear. Thanks for the feedback.. Happy reading!!!
it is a useful article for developers
Thank you for feedback