1

The lastest ECMAScript standard have 7 data types :

  • Primitive :
    • Boolean.
    • null.
    • undefined.
    • String.
    • Number.
    • Symbol ( new in ECMAScript 2015 ).
  • Non - Primitive :
    • Object.

So, what is the difference between Primitive data type and Non - Primitive data type ? Many thanks for support.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 2
    See https://www.ecma-international.org/ecma-262/9.0/index.html#sec-terms-and-definitions : *"A primitive value is a datum that is represented directly at the lowest level of the language implementation."* and "*An object is a collection of properties and has a single prototype object. The prototype may be the null value."* . In other words, an object is a collection of other values. – Felix Kling Jan 06 '19 at 19:34
  • Have a look here: https://medium.com/@junshengpierre/javascript-primitive-values-object-references-361cfc1cbfb0 – faerin Jan 06 '19 at 19:34
  • @HereticMonkey It's the same question but got terrible answers. – Bergi Jan 06 '19 at 19:47
  • Only objects can have properties. – Bergi Jan 06 '19 at 19:48
  • @Bergi Feel free to answer either with a non-terrible answer and set the dupe accordingly.... – Heretic Monkey Jan 06 '19 at 20:01
  • Isn’t there also a non-primitive array? – Jack Bashford Jan 06 '19 at 20:05
  • @JackBashford All arrays are non-primitive values. It's just a subclass of `Object` with some special property behaviour. It's less special than function objects. – Bergi Jan 06 '19 at 20:13
  • Oh, so an array is technically an object in JavaScript? I see how it’d work now — thanks @Bergi! – Jack Bashford Jan 06 '19 at 20:16

2 Answers2

0

The main difference between them is that Primitive data types are immutable values and Objects on the other hand are mutable.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
ellipsis
  • 12,049
  • 2
  • 17
  • 33
  • 5
    Objects can be made immutable though. I'd say the main difference is that objects are basically collections of other values. – Felix Kling Jan 06 '19 at 19:36
0

The difference between primitive and non-primitive / complex data types in JavaScript is that the complex data types (array and object) can store other kinds of data - they can store both complex and primitive data types, as you can have nested / multidimensional arrays and objects, but these can both contain all primitive data types as well.

There’s a very simple way to think about that mouthful as well:

If it has brackets, it holds anything.

Hopefully this helps!

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79