Strange behavior with JavaScript

JAVASCRIPT

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 provide your feedback in the comment section. Follow me

C# Publication, LinkedIn, Instagram, Twitter, Dev.to, Pinterest, Substack, Wix

#JavaScript #JavascriptDevelopment #Js #JavascriptTips #Programming

Recent Posts

See All

Design Pattern – Adapter

#Aspnetcore #AdapterDesignPattern #Csharp #DesignPatterns #Dotnet According to Gang of Four, the Adapter Pattern converts the interfaces of a class into interfaces that the client requires. In other w