Typeof operator


#1

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?


#2

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


#3

type of X will be "object"'.

"array" is not a type.

To test if an object is an array,

Array.isArray(X)    // true

#4

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


#5

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


#6

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.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array


#7

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