简体   繁体   中英

what is difference between data type and primitive type in javascript?

I found data type and primitive type in books.

  • Boolean
  • Number
  • String
  • Null
  • Undefined

Are they the same thing or different?

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.

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.

Types represent groups or sets of values. Some have only one value, eg 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, eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM