Tuesday 30 January 2018

Reason why JavaScript is Dysfunctional

Reasons why JavaScript is Dysfunctional :- 

1) typeOf operator weird behavior -  

- The typeOf operator tells us which built-in type a variable is so when we check it with <null> it returnsobject and when check type of <undefined> it returns undefined. It means JavaScript considers undefined as datatype in this case which is a flaw.

Note :- In this example I have used online code editor JS Bin

typeof null; // object
typeof undefined; // undefined


Some other examples -

2) Equal Operator Problem

To check whether 2 variables are equal there is 2 operators in JavaScript. These are:-
1) Equal ( ==  )
2) Strict Equal ( === )
Problem  - The == (or !=) operator performs an automatic type conversion. It means it considers string of 1 equal to numeric 1.


Other examples -

So its often recommended to use Strict Equal ( === ) whenever possible The === (or !==) operator will not perform any conversion. It compares the value and the type, which could be considered faster than ==.

3) Don’t use delete to remove an item from array

Delete actually replaces the item with undefined instead of the removing it from the array. If you check the array length before and after deletion it remains the same. So its recommend to use splice instead of using delete to delete an item from an array.

4) Floating point problems

0.1 + 0.2 === 0.3 // is false
0.1 + 0.2 // is 0.30000000000000004
9007199254740992 + 1 // is 9007199254740992
9007199254740992 + 2 // is 9007199254740994
This happen because JavaScript numbers are floating points represented internally in 64 bit binary according to the IEEE 754 standard.

To resolve this issue it is recommended to use toFixed() or toPrecision()

No comments:

Post a Comment

Chapter : 1 - First code in Node JS Previously I have written a blog about Getting Started with Node JS and its installation. Now lets s...