JavaScript: Confusing and unknown concepts you must know.

[sgmb id=3]

 

As we all are aware of the confusion that Javascript creates sometimes. Lets talk about few of them and why they happen.

010===8 is true

why 😕 ? When you prepend 0 before any number Javascript consider it as octal. Thats why 😀

010 !== 10  

null is not an object

Null is not an object in Javascript but when you do

typeof null

It will give object why so?

It is a known bug in Javascript and is not solved because it can break the other code. [taken from this chapter]

try this

null instanceof Object

it will give false, hence verify.

{} !== {}

Why? because {} is an object and object can never be equal another object as they have unique identifiers.

Number(null) = 0

Yes this happens here, when you use Number function to convert null to number it is converted to 0.

5 + null = 5

This is equal to

5 + Number(5) = 5+ 0 = 5

String('abc')==='abc'  is true but new String('abc')==='abc' is false

String(‘abc’) is primitive type but new String(‘abc’) is and object. Since Object cannot be equal to anything except itself thus it is false.

Number(new Number(123)) is 123, String(new String('abc')) is abc but Boolean(new Boolean(false)) is true.

Why?

Because in Boolean function we get Object and Boolean of anything except false,”,NaN, null, undefined everything else is true.

[1, 2] + [3] = 1,23

Converts array to string and then appends. It works like this

String([1,2]) + String([3])

Overloading of operators is not allowed in Javascript.

Here are few for you to find out, comment below if you get the answer

NaN !==NaN

Why? Figure out this and the next one and post in comments 🙂

var null; // syntax error 
null = 0; // another error 
var undefined; // not an error 
undefined = null; //not an error

Javascript is powerful and vulnerable at the same time. Simple tip if you dont want it to behave different try using

'use strict'

This will make javascript to  start throwing error where it use to pass silently in sloopy mode.

Read about strict and lenient equality in javascript

https://www.learnsteps.com/javascript-increasing-performance-by-handling-scopes-smartly/

https://www.learnsteps.com/javascript-increasing-performance-using-dynamic-loading/

https://www.learnsteps.com/javascript-difference-between-strict-and-lenient-equality/

https://www.learnsteps.com/javascript-increasing-performance-u

Will come with more such interesting Javascript magics and confusion stay updated and subscribe.


Gaurav Yadav

Gaurav is cloud infrastructure engineer and a full stack web developer and blogger. Sportsperson by heart and loves football. Scale is something he loves to work for and always keen to learn new tech. Experienced with CI/CD, distributed cloud infrastructure, build systems and lot of SRE Stuff.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.