isArray method and typeof
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.
let array = ["JS", "Startup"];
In the next step, we print the typeof value of array using console log.
Any idea, what should be the output of above code statement.
let array = ["JS", "Startup"]; console.log(typeof array); // output => 🧐 ? console.log(Array.isArray(array)); // output => 🧐 ? // Made with ❤️, by @jsstartup
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
function", and “
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 –
- JS Quiz – for loop iterate array in two ways
- JS Quiz – array element duplicate count with for loop
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.