DEV Community

Wincy H
Wincy H

Posted on

what are primitive values and common data type?

All types except Object define immutable values represented directly at the lowest level of the language. We refer to values of these types as primitive values.

Every primitive value except Object can be changed to any built in type, after initial assigned.

Primitive values table:

Type typeof return value Object wrapper
Null "object" N/A
Undefined "undefined" N/A
Boolean "boolean" Boolean
Number "number" Number
BigInt "bigint" BigInt
String "string" String
Symbol "symbol" Symbol
// Initial a var and immutable values later
var a;
// "undefined"
typeof a;                      
typeof a === "undefined";      // true

// boolean
a = true;
typeof a === "boolean";        // true

// number
a = 100;                       
typeof a === "number";         // true

// Bigint
a = 100n;
typeof a === "bigint";         // true

// String
a = "100";
typeof a === "string";         // true

// Symbols
typeof Symbol() === "symbol";
typeof Symbol("foo") === "symbol";
typeof Symbol.iterator === "symbol";

// Object is not immutable, but can contain all primitive values type
// Object can be accessed with doted notation or bracket notation
var Obj = {
   // properties a, b, c
   a: 1,              // typeof Obj["a"] === "number"
   b: "hello world",  // typeof Obj["b"] === "string"
   c: true,           // typeof Obj["c"] === "boolean"
   d: { e: 1n }       // typeof Obj.d === "object" 
                      // typeof Obj.d.e === "bigint"
}

// array is typeof Object with numrically indexed positions
var arr = [
   1,                // typeof arr[0] === "number"
   "hello world",    // typeof arr[1] === "string"
   true,             // typeof arr[2] === "boolean"
   {e: 1n}           // typeof arr[3] === "object" 
                     // typeof arr[3]["e"] === "bigint"
]

// null is special in typeof please refer to two documentations for detail
a = null;                      
typeof a === null;             // true
typeof null === "object";      // true

Enter fullscreen mode Exit fullscreen mode

Reference:
JavaScript data types and data structures
Javascript typeof

Top comments (0)