What're Primitive Data Types in JavaScript?

In JavaScript, a primitive is a data type that is not an Object. There are seven primitive data types in JavaScript:

  1. string;
  2. number;
  3. bigint;
  4. boolean;
  5. symbol;
  6. undefined;
  7. null.

Please note that for legacy reasons, typeof null gives you 'object' (as opposed to 'null'). null is actually a primitive type.

In JavaScript, a primitive value is:

  • Immutable;
  • Passed by value;
  • Compared by value;
  • Copied by value.

This is in contrast to an object (which is mutable, and is passed, copied and compared by reference).

In JavaScript, all primitive data types have their object equivalents (except null and undefined) that wrap around the primitive values:

  • String for the string primitive;
  • Number for the number primitive;
  • BigInt for the bigint primitive;
  • Boolean for the boolean primitive;
  • Symbol for the symbol primitive.

These wrapper objects allow you to operate on primitive values by calling object methods on the value. In some cases JavaScript automatically coerces/coverts primitives to its object counterparts. For example, this happens when:

  • You call an object method on a primitive value (e.g. 'foo'.toUpperCase()), or;
  • You pass primitive as the first argument to Function.prototype.call() or Function.prototype.apply() in non-strict mode.

In such cases, the wrapper object is thrown away once the operation is complete.

In case you want to get the primitive value from a wrapper object, you can do so by using the valueOf() method.


This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.