0

I saw the following statements at W3school.

enter image description here

I came from a beginner java background, so the data types and types of objects in javascript is confusing me quite a bit. I have a few questions:

  1. what does "object" data type consist of?
  2. What is the difference between data type "object" and types of objects "Object"?
  3. when I tried to find out the data type of "null" by typing typeof null, my IDE tells me that it is of type "object", not of type "null", which is what I expected.
halfer
  • 19,824
  • 17
  • 99
  • 186
Thor
  • 9,638
  • 15
  • 62
  • 137
  • 3
    I think that information is confusing, if not plain wrong. Functions are objects as well. And there're many other objects not mentioned here: Number, String, RegExp... – Álvaro González Aug 23 '16 at 08:14
  • 4
    "I saw the following statements at W3school." --- the first rule, don't use w3schools. Never. **NEVER**. – zerkms Aug 23 '16 at 08:16
  • Try the [MDN docs](https://developer.mozilla.org/en/docs/Web/JavaScript/Data_structures) instead. – Tigger Aug 23 '16 at 08:26
  • if w3school is something I should avoid, what resource should I use to learn javascript instead? does anyone have any recommendations? – Thor Aug 23 '16 at 12:55
  • Hi there Tony. I am seeing a lot of repeated "filler" text in your questions - just so you know, we tend to trim this out, so if you can omit `Could someone please help me out?` and `Thanks in advance for any help` and `I don't know where else to ask this` it would save us quite a bit of editing work. Thanks! – halfer Aug 24 '16 at 21:29
  • @zerkms - Why we should not use W3Schools? – aagjalpankaj Jan 07 '19 at 11:33
  • 1
    @aagjalpankaj because of the quality of the articles they publish – zerkms Jan 07 '19 at 19:15

2 Answers2

1

typeof will always returns the primitive type. ('string', 'number', 'object', etc.).

An object is a primitive type structure, an unordered list of primitive data types stored as a series of name-value pairs.

Object is the constructor of an object primitive.

var test = {};
console.log(typeof test);             // object
console.log(test instanceof object)   // false
console.log(test instanceof Object)   // true

According to this, to check for a Date or an Array, using typeof will returns object:

typeof [];           // object
typeof new Date();   // object

To test if the object is actually a Date or an Array, you can use instanceof:

[] instanceof Array;          // true
new Date() instanceof Date;   // true

Regarding the typeof null, it's a bug in ECMAScript according to the MDN and should be null. The bug is detailed in this answer. A fix has been proposed via an opt-in, unfortunately the change was rejected due to code using this specific 'bug' to test for null.

Community
  • 1
  • 1
HiDeoo
  • 10,353
  • 8
  • 47
  • 47
1

W3Schools (not to be mistaken with W3C (World Wide Web Consortium)) often contains very inaccurate information. Discussion: Why not w3schools.com. Instead, use information provided by MDN for example.

Here is an article about data types in Javascript you will find useful: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures

To answer your questions:

Objects are just a collection of properties: combinations between a key (string, number, objects ...) and a value: other objects, functions, strings, numbers, ... Arrays (which has some custom things like indexation etc) and Dates are objects.

There is no such thing as object and Object.

The reason why typeof null returns object is because of old specs that were never changed. Why is typeof null “object”?

Community
  • 1
  • 1
Simon Backx
  • 1,282
  • 14
  • 16
  • "W3School isn't an official source" --- to be fair the MDN in no way an "official source" either. Only the standard is. – zerkms Aug 25 '16 at 06:52