4

https://developer.mozilla.org/en-US/docs/Glossary/Primitive In JavaScript, a primitive (primitive value, primitive data type) is data that is not an object and has no methods. There are 6 primitive data types: string, number, bigint, boolean, undefined, and symbol. There also is null, which is seemingly primitive, but indeed is a special case for every Object: and any structured type is derived from null by the Prototype Chain.

but in Javascript the definitve guide, it says Any Javascript value that is not a number, a string, a boolean, a symbol, a null, or undefined is an object.

So which one is right? Is null an object or not.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
ChromeBrowser
  • 209
  • 2
  • 6
  • Does this answer your question? [Check if a value is an object in JavaScript](https://stackoverflow.com/questions/8511281/check-if-a-value-is-an-object-in-javascript) – Tân Nov 12 '20 at 03:42
  • I'm still confused. The link above (which has 1500 upvotes) says null is an object, and the answer below says its a primitive. – Rick Nov 12 '20 at 03:54
  • 3
    Although `typeof null` will return "object", `null` is a primitive value of the type "null" (and the only value of that type.) Check out this question for some insight into the issue with the `typeof` operator: https://stackoverflow.com/questions/18808226/why-is-typeof-null-object – Cat Nov 12 '20 at 04:15

3 Answers3

8

When in doubt, read the official specification, which is essentially the Word of God for JavaScript.

A primitive value is a member of one of the following built-in types: Undefined, Null, Boolean, Number, BigInt, String, and Symbol; an object is a member of the built-in type Object; and a function is a callable object. A function that is associated with an object via a property is called a method.

So, yes, null is a primitive value.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
2

As you can see null, Arrays, and Objects all output type 'object' when passed to typeof.

console.log(typeof null); console.log(typeof []); console.log(typeof {});

This, however, is not the same as the Object type.

If you wish to test for types like you're thinking and as defined by ECMAScript, you might want to check this out.

function getType(mixed){
  if(mixed === null){
    return 'null';
  }
  else if(mixed instanceof Array){
    return 'array';
  }
  return typeof mixed;
}

class Test{
  constructor(){
  }
  method(){
    return this;
  }
}
function ConstructorTest(){
  let privateVarsOk;
  this.method = ()=>{
    return this;
  }
}
const test = new Test; constructorTest = new ConstructorTest;

console.log(getType(null));
console.log(getType('4'));
console.log(getType(4));
console.log(getType(['a', 2, 'c']));
console.log(getType({}));
console.log(getType(getType));
console.log(getType(function(){}));
console.log(getType(()=>{}));
console.log(getType(Test));
console.log(getType(test));
console.log(getType(ConstructorTest));
console.log(getType(constructorTest));
console.log(getType());

Notice that classes are actually functions though, as they do no introduce a new hierarchy.

Akaisteph7
  • 5,034
  • 2
  • 20
  • 43
StackSlave
  • 10,613
  • 2
  • 18
  • 35
  • 1
    There's a difference between lowercase `object` from `typeof` and uppercase `Object` which is a builtin `constructor` function. e.g. `null instanceof Object` is `false` and `[].constructor` is `Array`. – Bryan Grace Nov 12 '20 at 05:53
  • @BryanGrace, notice that I just test for `mixed === null` since I have that argument there, so it will return what OP wants. I know that `instanceof` is not the same as `typeof` already. By the way, a `constructor` is a `function`. – StackSlave Nov 12 '20 at 12:31
0
var nullVar=null;
console.log("Data type of nullVar is :"+ (typeof nullVar));

o/p;-Data type of nullVar is :object

Null is primitive datatype but it returns object ... So, null return bogus return value ..

Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39