3

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?

5
  • 1
    That's how it works Commented Jul 11, 2020 at 19:36
  • 1
    Because arrays are object. You need to use Array.isArrayinstead to check whether something is an array. Commented Jul 11, 2020 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). Commented Jul 11, 2020 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? by @T.J.Crowder Commented Jul 11, 2020 at 19:47
  • for more information study the YDKJS book,book3. chapter3 github.com/getify/You-Dont-Know-JS/blob/1st-ed/… Commented Jul 11, 2020 at 19:53

3 Answers 3

7

Try using the instanceof operator

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

const obj = {};
console.log(obj instanceof Array); // false

Sign up to request clarification or add additional context in comments.

Comments

2

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().

1 Comment

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 codeburst.io/… and the specification.
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

9 Comments

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”.
Ok I meant to say almost. Of course there are primitives.
Also there are object wrappers
Then ‘everything’ is incorrect.
var str = new String('string')
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.