2

Either we check type of array or object it always print object in JavaScript. Why so?

let arr=[1,3,4];
let obj={1:"44",num:44};

console.log(typeof(arr)) //object
console.log(typeof(obj)) //object

Is there any way to see typeof(array) as array?

SOURABH GUPTA
  • 35
  • 2
  • 5
  • 1
    [That's how it works](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof#Description) – pishpish Jul 11 '20 at 19:36
  • 1
    Because arrays are object. You need to use `Array.isArray`instead to check whether something is an array. – briosheje Jul 11 '20 at 19:36
  • Is there any way to check whether something is an object but not array instead of giving two conditions like `typeof(val)=="object" && !Array.isArray(val)`. – SOURABH GUPTA Jul 11 '20 at 19:39
  • 2
    _'When reaching for `instanceof` or `typeof` or whatever, ask yourself: Do you really care? How 'bout looking to see if the object seems to have the things on it you need (feature detection) rather than worrying about what it is? This is usually called "duck typing," from the phrase "If it walks like a duck and talks like a duck..."'_ - Quote from the blog post [Nifty Snippets: Say what?](http://blog.niftysnippets.org/2010/09/say-what.html) by @T.J.Crowder – Andreas Jul 11 '20 at 19:47
  • for more information study the YDKJS book,book3. chapter3 https://github.com/getify/You-Dont-Know-JS/blob/1st-ed/this%20%26%20object%20prototypes/ch3.md – Narges Ghasemi Jul 11 '20 at 19:53

3 Answers3

4

Try using the instanceof operator

const arr = [];
console.log(arr instanceof Array); // true

const obj = {};
console.log(obj instanceof Array); // false
AKT
  • 942
  • 6
  • 16
lostsource
  • 21,070
  • 8
  • 66
  • 88
1

Because an array is technically a type of object - just with certain abilities and behaviors, for instance additional methods like Array.prototype.push() and Array.prototype.unshift(). Arrays are regular objects where there is a particular relationship between integer-key-ed properties and the length property.

To determine whether you have an array specifically, you can use Array.isArray().

Muirik
  • 6,049
  • 7
  • 58
  • 116
  • The language/specification *could* have been written to support it.. and alas, it was not and here we are. *The rationalization of an array “being an object” being the crucial designator here is tenuous*: after all, a function *is* an actual Object type (not a primitive), yet it is a “function” in relation to the typeof operator. So the reason is due to an arbitrary design decision stemming from the very inception versions. - See https://codeburst.io/javascript-essentials-types-data-structures-3ac039f9877b and the specification. – user2864740 Jul 11 '20 at 21:21
0

In JavaScript, almost everything is an Object.

It uses prototype chain to achieve inheritance.

you can just console.log( [] ) and see the prototype part to see that it inherit from objects.

Here is a simple way to make an your own Array.

function MyArray(){
Object.defineProperty(this, 'length', {
    value: 0,
    enumerable: false,
    writable: true,
    })
}

MyArray.prototype.push = function(elem){
    this[this.length] = elem
    this.length++
    return this.length
}


MyArray.prototype.isMyArray = function(instance){
    return instance instanceof MyArray
}


var arr = new MyArray()
arr.push(1)
arr.push(2)
arr.push(3)


console.log('instance', MyArray.prototype.isMyArray( arr ))
// instance true
console.log('object', typeof arr)
// object object
albseb
  • 169
  • 5
  • The assertion is incorrect: ‘everything’ (much less every value) is *not* an object in JavaScript. As a trivial example, “hello” is a primitive value of type string. It can be implicitly promoted to an object of type String. This distinction is important to not “wash away” as it applies to many ECMAScript rules, including === etc. Search for “ECMAScript Specification”. – user2864740 Jul 11 '20 at 20:29
  • Ok I meant to say almost. Of course there are primitives. – albseb Jul 11 '20 at 20:35
  • Also there are object wrappers – albseb Jul 11 '20 at 20:35
  • Then ‘everything’ is incorrect. – user2864740 Jul 11 '20 at 20:35
  • var str = new String('string') – albseb Jul 11 '20 at 20:36
  • https://tc39.es/ecma262/#sec-terms-and-definitions-string-type it says member of Object type. – albseb Jul 11 '20 at 21:10
  • The String type is different from the string type (case-sensitivity is important in context). Only one of these types represents an immediate Object, which can wrap the other implicitly. – user2864740 Jul 11 '20 at 21:19
  • I am aware that constructors have first letter capital. I did mention there are object wrappers. I am just saying that it still has an object wrapped around it. if it only contained value, then how do you explain str.length, str.toLowerCase ? it has methods attached to it. that is not a behavior of something that only contains a value. So i think we both are on the same page. There are primitives, but its also to be understood that these have their Object wrappers also. I have not looked at internals to see if they use these object wrappers to access the methods in it. – albseb Jul 11 '20 at 21:36
  • and this is going off topic, I had updated the answer to almost everything. so I think we can end the discussion here. – albseb Jul 11 '20 at 21:37