JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – isArray method and typeof intend is different

JavaScript Quiz – isArray method and typeof intend is different

isArray method and typeof

JavaScript array is collection of different type of value with different-different data type but still array has it’s own data type and even we have isArray method to check if array is array.

Let’s look at the basic syntax of isArray & typeof

Array.isArray(value)

// OR

typeof operand
typeof(operand)

But as you know that, typeof return a string and Array.isArray() return boolean.

JavaScript Question

In this javascript quiz, find out does isArray method and typeof intend is different because isArray say different and typeof return different.

First, we declare a variable array using let keyword and assign an javascript array to it.

let array = ["JS", "Startup"];

In the next step, we print the typeof value of array using console log.

console.log(typeof array);

Now, we use javascript array method to check if array isArray. which return boolean value.

console.log(Array.isArray(array));

Any idea, what should be the output of above code statement.

JavaScript Quiz Test

Here is the full javascript code snippet for you to solve.

let array = ["JS", "Startup"];

console.log(typeof array);
// output => 🧐 ?

console.log(Array.isArray(array));
// output => 🧐 ?

// Made with ❤️, by @jsstartup

Answer – “object“, true

Click To Run Code

Explanation

Now, you know the answer is “object“, true. But why, if first answer is “object” the another one should be false or vice-versa. Let me explain

InShort, yes both show different intend but still they are right.

First, typeof give an “object” because array belongs to object category like class. Currently, typeof has 8, and only 8 value types

"undefined", “boolean", “string", “number", “bigint", “symbol", “function", and “object".

And the isArray method is special which identifies that the variable is an array. As, we clearly see that it return true.

At last, That’s why we get an answer “object”, true

Let me know in the comment section, what you think about it.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JavaScript Quiz – can divide assignment operator work in one go
  2. JavaScript Quiz – do act then while checking falsy
  3. JS Quiz – for loop iterate array in two ways
  4. JS Quiz – array element duplicate count with for loop

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

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

Back to top
0 Shares
Share via
Copy link