JavaScript Object and multiple ways to create it

In JavaScript, objects are king.

In JavaScript, almost “everything” is an object.

  • Booleans can be objects (if defined with the new keyword)
  • Numbers can be objects (if defined with the new keyword)
  • Strings can be objects (if defined with the new keyword)
  • Dates are always objects
  • Maths are always objects
  • Regular expressions are always objects
  • Arrays are always objects
  • Functions are always objects
  • Objects are always objects

All JavaScript values, except primitives, are objects.

  • string
  • number
  • boolean
  • null
  • undefined

Primitive values are immutable

Object Creation

This is the simplest way to create an empty object.

This method creates a new object extending the prototype object passed as a parameter.

This is equivalent to Object.create(null) method, using a null prototype as an argument.

What the new operator does is call a function and setting this of function to a fresh new Object, and binding the prototype of that new Object to the function's prototype. As is:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhay Jain

Abhay Jain

Developer with 3 yrs of industrial experience in developing scalable web applications.