Primitives and Objects

Primitive types:

  • String
  • Number
  • Boolean
  • Undefined
  • Null
  • Symbol
  • Symbol is a primitive data type of JavaScript, along with string, number, boolean, null and undefined. It was introduced in ECMAScript 2015. Once you create a symbol, its value is kept private and for internal use.

Reference types. Objects:

  • Object
  • Array
  • Function
  • Date
  • RegExp

Example

“hello”.length

But how?

new String(“hello”).length;

notice the constructor

type of obj –> “object”

typeof String(“hello”) –> “string”

obj.valueOf(); –> “hello”
typeof obj.valueOf(); –> “string”

Only way to create object is by using new String. All others will give you a string.

Similary,

//notice the constructor

typeof num; –> “object”
typeof 10 –> “number”

typeof Number(10) –> “number”
typeof num.valueOf() –> “number”

remember that our num is an object

typeof sum; –> “number”
typeof num; –> “object”

Even though num was used in a numeric operation, it did not get converted to number.

Cannot assign properties and methods to Primitives

For example

If we change num to object, then we can assign property and methods: