![Why typeof( [ ] ) Returns "object" in JavaScript](/_next/image?url=https%3A%2F%2Fmedia2.dev.to%2Fdynamic%2Fimage%2Fwidth%3D1200%2Cheight%3D627%2Cfit%3Dcover%2Cgravity%3Dauto%2Cformat%3Dauto%2Fhttps%253A%252F%252Fdev-to-uploads.s3.amazonaws.com%252Fuploads%252Farticles%252Fsl6ri74fe79qwk0el359.png&w=1200&q=75)
Why typeof( [ ] ) Returns "object" in JavaScript
If you’ve been writing JavaScript for even a short time, you’ve probably seen this: typeof [] // "object" And your brain immediately goes: Wait… what? That’s clearly an array. Why is JavaScript lying to me? Let’s break this down properly — not just the surface-level explanation, but the real reason this happens. First: JavaScript Has Only a Few Real Types In JavaScript, typeof only returns one of these values: " undefined " " boolean " " number " " string " " bigint " " symbol " " function " " object " Notice anything missing? There’s no "array" . That’s not an accident. Arrays Are Objects. Period. In JavaScript, arrays are not a separate primitive type. When you write: const arr = []; You’re actually doing: const arr = new Array (); And Array is a constructor that creates an object. So technically: typeof [] // "object" is completely correct. What Makes Arrays Special Then? If arrays are just objects, why do they behave differently? Because arrays are specialized objects with: Numeric
Continue reading on Dev.to Webdev
Opens in a new tab




