July 14, 2019

Five Tricky JavaScript Questions!!

Five Tricky JavaScript Questions!!

Welcome to Tekiana Blog. JavaScript is a language that was written in just a couple of weeks!! JavaScript devs have surplus aha moments. In today's post, We are going to see simple yet tricky JavaScript Questions.

1. What is the output of console.log(+"2"+2)

  • 4
  • Error
  • 22

Yes, The Answer is 4  . The reason is , in javascript, unary operator "+" placed before a string will result in an Integer.  Even, console.log(2 + + "2") will result in same 4!.

  1. let arr=[];
    arr.length = ?
  • 1
  • Array Out Of Bounds Exception
  • 101
  • 100

The answer is "101". Surprised ? In Javascript, the arrays are different from classical programming languages. If you see the array structure, it will be as below. In Javascript, array can accept element assignment at any index and returns length as lastIndex+1.

3. What is the output of following code snippet?
let digitsArray=[1,2,3,4];

  • 3
  • 4
  • Error

The answer is 4! Reason being the delete operator just deletes element at index "2". Since In Javascript, Array.length returns lastindex+1 , We get the answer 4. The digitsArray is shown after delete operation below. To avoid this behaviour, array.splice should be used.

4. !!String("false") = ?;

  • false
  • true
  • "TRUE"
  • "FALSE"

The Answer is true. In Javascript, any non-zero value result in true.

5. typeof NaN =?

  • object
  • function
  • string
  • number

We all know NaN stands for "Not a Number". However, it's type is "number" !!!

That's it for today. For more JS questions, take our assessment and test yourself!

                                      Happy Coding :)