Typeof operator

I was wondering, if I declare an array X in JavaScript and enter a number of elements of different data types and then use the typeof operator as follows:

var X = [2, ‘string’, 3.5];
typeof X;

Will the output be ‘array’? If so how do I identify the data type of the elements of the array? Moreover, typeof identifies all numbers as ‘number’, is there any way I can manipulate this operator or use any other operator to differentiate between an integer and a float number?

js doesn’t have a separate integer type

How would you print the second element of your array? Getting the type wouldn’t be so different from that

1 Like

type of X will be "object"'.

"array" is not a type.

To test if an object is an array,

Array.isArray(X)    // true
1 Like

Ah right yes, I dont know how I missed that, so I will just do
typeof X[1];
well thanks for the explanation

But aren’t objects the ones with keys, key values and all that?

In JavaScript everything is an object, starting with the window itself.

console.log(window.X)    // [2, 'string', 3.5]

The JavaScript Array object is a global object that is used in the construction of arrays; which are high-level, list-like objects.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.