0

I found data type and primitive type in books.

  • Boolean
  • Number
  • String
  • Null
  • Undefined

Are they the same thing or different?

Saurabh Chandra Patel
  • 12,712
  • 6
  • 88
  • 78
  • 2
    http://stackoverflow.com/questions/8643276/object-vs-primitive – Saar Oct 06 '15 at 04:18
  • 1
    This can be easily googled, try to give more effort researching before asking here. – TwilightSun Oct 06 '15 at 04:20
  • See [*ECMAScript Data Types and Values*](http://www.ecma-international.org/ecma-262/6.0/#sec-ecmascript-data-types-and-values), also [*primitive value*](http://www.ecma-international.org/ecma-262/6.0/#sec-primitive-value). – RobG Oct 06 '15 at 04:26

3 Answers3

0

All of the types you listed are primitive types, a subset of data types. In javascript, data types also include objects which are not a primitive type, although it is still a data type. Objects are an example of a data type that would be considered non-primitive.

I'm sure that if you continue reading, your book will lead you through that subject.

Andrew
  • 1,322
  • 14
  • 20
0

There are 7 data types in ECMA standard. Six of them are Primitives and the other is Object.

Primitives are the most basic forms the data can be represented in Javascript. They cannot be sub-divided any further in language constructs.

In contrast, the Object is something that can be comprised of one or more primitive types.

Look at the difference between following examples:

var n = 5;    //Holds data equivalent to number 5. A primitive.

var a = [10, 20, 30] //Holds 3 separate number values. This array is an object.

var o = {name: "John", age: 25}   //Holds two types of values addressed by a specific name. This is an object.

There, in fact, are lot to talk about the characteristics of these types. Your best introductory guide for this is : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures

Also the experimentation is your best teacher when learning the fundamentals of any language.

Charlie
  • 22,886
  • 11
  • 59
  • 90
  • You're confusing the OP. Primitives aren't a "Type" as defined by the specification, they are a value that can be one of a number of Types. `'a'`, `3` and `true` are primitive **values** that are of **Type** string, number and Boolean respectively. – RobG Oct 06 '15 at 04:47
  • MDN excerpts ECMA as declaring that "There are seven data types and 6 of them are primitives. The other is Object. However I fine tuned my answer to give rise to this concept. – Charlie Oct 06 '15 at 05:49
  • That statement is accurate, but does not mean that primitive is a Type. Primitive is a kind of value. There are two kinds of value: primitive and reference. Primitive values can be of 6 types (those listed), references can be of 1 type (object). Consider an object property, which holds a value. That value can be a primitive or a reference. If it's a primitive, then it's one of 6 Types. If it's a reference, it can only be of one Type. – RobG Oct 06 '15 at 14:08
  • What you mean is the type of a value could either be primitive or reference. JS is not a strictly typed language and as such the variables don't have an explicit type. I explained the same thing in terms what the user has so far understood. – Charlie Oct 06 '15 at 14:46
  • I said primitive is a **kind** of value, not a Type. Primitive is not a language [*Type*](http://www.ecma-international.org/ecma-262/6.0/#sec-ecmascript-data-types-and-values) as defined in the specification (ECMA-262), nor are primitives variables. Variables hold values. Values have a Type. Values are also broadly categorised as [*primitives*](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-primitive-value) (6 Types fall in to that category) or references (which is a specification type used for variables that reference objects, which might be Functions, Arrays, Objects, etc.). – RobG Oct 07 '15 at 03:54
  • You can simply put "primitive is a category that certain values fall under". But you know all these complications are too much for a person who tries to understand the JS data types - although there is no such thing call a data type in JS. Sometimes you have to teach 5 /2 = 2 to a kindergarten student rather saying it is 2.5. – Charlie Oct 07 '15 at 04:12
  • "*…although there is no such thing call a data type in JS*". Perhaps the authors of the language specification should have been told that before they wrote the part titled [**ECMAScript Data Types and Values**](http://ecma-international.org/ecma-262/6.0/index.html#sec-ecmascript-data-types-and-values). – RobG Oct 07 '15 at 08:37
  • *"Primitive is not a language Type as defined in the specification (ECMA-262),"* Yeah! They should have been told about this too ;) – Charlie Oct 07 '15 at 10:39
0

Types represent groups or sets of values. Some have only one value, e.g. the Undefined Type has exactly one value: undefined, the Null Type also has exactly one value: null. The Boolean Type has two values: true and false.

Other Types have lots of possible values, such as the String Type and Number Type.

A primitive value represents a value at the lowest level, e.g. 3 is a primitive value of the Number Type, "a" is a primitive value of the String Type.

Objects belong to the Object Type, they are not primitives. Objects may have properties whose values are either primitives or references to other objects.

RobG
  • 142,382
  • 31
  • 172
  • 209