Tricky Parts of JavaScript [Part 2]

in javascript •  4 months ago

Welcome to the 2nd part of JavaScript Tricky Part. In this post we will be seeing some browser specific stuff as well as some number/strings/boolean manipulation stuff. These posts are to teach you the basic functioning of JavaScript so that you can tackle any complicated problem in future. Here we begin...

Q1. What is undefined x 1?

Ans. undefined x 1 is a way of representation that is performed by only chrome browser when you delete a array element. If you try the same thing on other browser then you will be shown undefined instead of undefined x 1. 


var arrayList = [1,2,3,4,5]
delete arrayList[2]

if you execute the above code then you will get your result as [1,2,undefined x 1, 4,5] for Chrome and [1,2,undefined, 4,5] for Firefox. Another important thing that I want to mention here is if compare undefined x1 and undefined then you will know there is no difference. 

//After deletion arrayList[2]

arrayList[2] === undefined // true, when the element is actually undefined x 1

arrayList[2] === undefined // true, when the element is actually undefined

Q2. How to know if an object is an array ?

Ans: Well, there are several solutions to know about if an object is an array or now but the best way to really distinguish is to use the toString() method invocation with call(). Let's take an example to learn more about it

var arrayItems = [12,3,4,5,5]

var toStringData =

// toStringData = "[object Array]"

As you can see the value of toStringData is a string with "Array" in it, just use it to know about the same. This method is very performant and efficient to use. Also, jQuery uses the same implementation to know about if an object is an array or not (jQuery.isArray(arrayItem))

Q3. What is the output of the following code?

var a = true
console.log(a + false) // 1
console.log(a + 0) // 1
console.log(a + true) // 2
console.log(a + " funnyman") // true funnyman
console.log(a + false + true) // 2

So there are a lot of thing to understand from above. That is ->

  1. Num + Num = Num (addition)
  2. Bool + Num = Num (addition)
  3. Bool + Bool = Num (addition)
  4. Bool + String = String (concatenation)
  5. String + String = String (concatenation)

I hope you got the point.

Feel free to ask if you have any doubts



Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!