JavaScript: Confusing and unknown concepts you must know.

JavaScript: Confusing and unknown concepts you must know.
4.71 (94.29%) 7 votes

  2 minutes

 

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 a Full Stack Web Developer and Blogger. Sportsperson by heart and loves football. He has experience with various frameworks in php, python and javascript. Loves to explore new frameworks and evolve with the trending technology.

Leave a Reply

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