Typeof array == object??

Author Topic: Typeof array == object??  (Read 15159 times)

Offline ngomaichiTopic starter

  • Trade Count: (0)
  • Newbie
  • *
  • Thank You 0
  • Posts: 5
  • Karma: 0
    • car racing games download for mobile
Typeof array == object??
« on: 05-25-2016, 04:04:17 »
dev in Firefox v37.0.2 on Max OSX Yosemite

var a = new Array('first', 'second', 'third', 'fourth')
alert(typeof(a)); // => 'object' (?)

I am developing a module that needs to distinguish the input argument type.
So I am using:
Code:
//constructor function code... etc...
var arrayInput = []
var objectInput = {}
switch(typeof(arg)) // arg == ['first', 'second', 'third', 'fouth', ]
   {
    case 'array': // for example ['first', 'second', 'third', 'fouth', ]
    alert('case array')
    arrayInput = arg;
    break;
    case 'string':
    break;
    case 'object': // for example {'find':'literal', 'type':'string', 'proc':function(a){/* callback code */}}
    alert('case object')
    objectInput = arg;
    break;
   }
// ... etc ...
array literal is coming up as 'object'
So I tried the code at the top of this post: specify an array using new Array, still an object type.
What is going on here, beside maybe problems with my code I haven't noticed yet?

Edit: It appears that the only way to distinguish an array from an object in this context is to look for
the length attribute (as the texts I use tell me, everything in javascript is an object, except primitives.
Yet strings have a length attribute and have a constructor function, and behave similar to arrays in the
sense that each char has an index). I won't complain.

Thanks for time and attention


Offline ngomaichiTopic starter

  • Trade Count: (0)
  • Newbie
  • *
  • Thank You 0
  • Posts: 5
  • Karma: 0
    • car racing games download for mobile
Re: Typeof array == object??
« Reply #1 on: 05-30-2016, 21:09:23 »
Can you help me???
--------------

Offline raghuramastrologer

  • Trade Count: (0)
  • Semi-Newbie
  • *
  • Thank You 0
  • Posts: 12
  • Karma: 0
  • Gender: Male
    • Astrologer Raghuram in Toronto
Re: Typeof array == object??
« Reply #2 on: 07-16-2019, 02:00:21 »
The typeof an array is an object. In JavaScript, arrays are technically objects; just with special behaviours and abilities. For example, arrays have a Array.prototype.length property, which will return the number of elements in the array

 

Related Topics

  Subject / Started by Replies Last post
0 Replies
1626 Views
Last post 08-19-2011, 01:56:39
by Walalayo
0 Replies
1366 Views
Last post 07-25-2013, 10:25:11
by Patrecia
0 Replies
1086 Views
Last post 12-21-2015, 02:27:50
by chinmay.sahoo
1 Replies
1292 Views
Last post 04-20-2016, 03:31:08
by TomClarke
0 Replies
1963 Views
Last post 08-11-2016, 07:22:02
by beingchinmay