There are numerous occasions where JavaScript behaves strangely.
I love the frameworks written on top of javascript but do not prefer to write code in javascript.
Please find below cases where javascript behave differently:
Scenario 1
As shown below, see addition and subtraction.
11+1 //12
'11'+1 //111
11-1 //10
'11'-1 //10 //What is hell? right?
Scenario 2
JavaScript is very loosely concerning data types.
let name="Sukhpinder";setTimeout(function(){ name="Singh"});
console.log(name); //Sukhpinder
Scenario 3
I was overwhelmed with the event loop because JS does not merely execute code from top to bottom, waiting when interrupted.
function add(a,b){ return a+b;}
//One parameter missing it wont work add(3); //Nan
//One parameter extra don't care add(1,2,3); //3
Scenario 4
Not only data types, everything is designed very loosely coupled in JavaScript.
0.1+0.2 //0.30000000000000004
2.3*100 //229.99999999999997
Scenario 5
Imprecise operations in Javascript, the same problem can be seen in other programming languages like Python.
console.log(!![]) //true
console.log([]==true) //false
Scenario 6
It does not know what to say about below, which is entirely illogical.
Array.prototype.push("Hello");
let empty=[];
console.log(empty[0]); //Hello
console.log(empty.length); //0
Overall, JavaScript is more accessible to write because for several reasons. On the other hand, JavaScript makes you feel insecure, as described in different scenarios above.
Thank you for reading and I hope you liked the article..!! Please share your thoughts in the comment section.
Top comments (8)
It is not. Most of the problems you're going to solve with JavaScript in any real-world application are from a completely different domain.
EDIT
I made this comment while the title of the post was "Why is JavaScript so hated?".
Yes you are right and I agree but I just wanted to share the use cases where javascript behaves strangely..!!
You should rename this article to « Strange Behavior in JavaScript ». JavaScript is in no way a hated language.
Done....changed. Thanks for your recommendation..!! Really appreciate it.
Scenario 6 blew my mind! I did not know that's how the Array prototype worked.
That actually inspired me to write this article...!! :D
Yes, Javascript has its quirks, but what you describe is defined functionality of the language. Reading the ECMAscript standards will fix your wrong expectations about it.
Sure. I will have a look..!!