1

I was experimenting with Uint16Array() (which is something new to me in js) and I created an array of 5 numbers like so:

var test = new Uint16Array(5);

then assigned a number to each cell.

then when I tried in my console typeof test returned "object" although when I did alert(test) the message-box returned [object Uint16Array].My question is how can I check the exact type of the variable / array "test" like alert returned?

Correct me if I'm mistaken but wouldn't this be more efficient, to use specific variable types for your data and furthermore is it supported from major browsers?

Thank you in advance.

0x_Anakin
  • 3,229
  • 5
  • 47
  • 86
  • possible duplicate of [How do I get the name of an object's type in JavaScript?](http://stackoverflow.com/questions/332422/how-do-i-get-the-name-of-an-objects-type-in-javascript) – Bergi Oct 09 '13 at 09:40

4 Answers4

3

If you want to specifically check whether a variable is of the type Uint16Array, you can use instanceof:

var test = new Uint16Array(5);
console.log(test instanceof Uint16Array)

will result in true:

For more on instanceof see What is the instanceof operator in JavaScript?

Community
  • 1
  • 1
CBroe
  • 91,630
  • 14
  • 92
  • 150
1

Array is actually an object so

typeof [1,2,3]

will return "object".

Now when you do alert(array) you're actually calling Object.prototype.toString() method which formats resulting string. In this case [object Uint16Array].

Typeof returns object because it is an object. Despite the fact you initialized Uint16Array - it is still an object :). Like for example:

function car() {}
var c = new car();
typeof c === 'object' // => true

Numbers, strings, objects etc. are generic data types. Look here for js datatypes.

An ugly method to check for exact type would be this:

array.toString().split(/\b/g)[3]

Why index 3 ? because returned string is [object type]; so the parts are like this:

  • 0 - [
  • 1 - object
  • 3 - space
  • 4 - type
  • 5 - ]
lukas.pukenis
  • 13,057
  • 12
  • 47
  • 81
  • yes I know it is still an object, so I suppose we could check for the exact type of the array by comparing the arrayName.toString() result? – 0x_Anakin Oct 09 '13 at 09:38
  • No, his alert is not using `Array.prototype.toString` - that would not have returned `[object Uint16Array]`. Not all objects have `toString`-methods that yield such strings - most prominently, plain `Array`s don't. – Bergi Oct 09 '13 at 09:47
1

You can test more precisely for type in Javascript using the triple equality operator (===) so that something like typeof(function() { }) === 'function' will return true. However, Uint16Array is a constructor that creates an Array object, therefore any derivative of that will be an object, as well.

As far as making it more efficient in browsers, you can count this kind of optimization as "trying too hard" because it's not going to save you much (if any) noticeable amount of speed or efficiency. There are, of course, caveats to this where your data is Large, but generally you won't see an improvement by programming towards type in Javascript. That's not to say it's worthless, just that it generally isn't going to be something worthwhile most of the time.

L0j1k
  • 12,255
  • 7
  • 53
  • 65
  • Thank you for your answer. Yes I know in general it is "trying to hard" but I was thinking a use case in large amount of data and memory limit in mobile applications. – 0x_Anakin Oct 09 '13 at 09:42
  • 1
    In that case it would likely be far more efficient to just write a native app that does the processing, or simply outsource the processing to a server built specifically to process data. Javascript is a pretty fast language already, but if resources in my budget were tight, I'd skip it and write a native app for a mobile device instead. That being said, if you absolutely are limited to local Javascript on mobile ONLY, then unless you're doing something like intense prime calculations or something, don't worry about it. I have no numbers to back this up, only experience... – L0j1k Oct 09 '13 at 09:45
  • ... So if you do some research and come up with some hard numbers, I'd love to be proven wrong, just to get a look at those numbers. :) – L0j1k Oct 09 '13 at 09:46
1

how can I check the exact type of the variable / array "test" like alert returned?

Use Object.prototype.toString to get the internal [[Class]] value of an object. Your alert seems to have used this method because Uint16Array.prototype does not overwrite it, as opposed to e.g. Array.prototype. Some examples:

Object.prototype.toString.call( {} ); // [object Object]
Object.prototype.toString.call( [] ); // [object Array]
Object.prototype.toString.call( new Uint16Array(5) ); // [object Uint16Array]
Bergi
  • 630,263
  • 148
  • 957
  • 1,375