Dev / JavaScript

Creating Objects in JavaScript


There are 2 ways to create objects in Javascript: literal notation and constructor notation.

I’ll demonstrate below how to create the same object using each method.

Literal Notation

var pig = {
  name: "Porkster",
  age: 5,
  size: "Large",
  favoriteFood: "Slop"

Constructor Notation

var pig = new Object(); = "Porkster";
pig.age = 5;
pig.size = "Large";
pig.favoriteFood = "Slop";

It actually depends on you which one you use. Obviously constructor notation requires more characters to type but remember that you can combine the two. You can start by using literal notation and just add properties later using dots:

var cow = {
  name: "Holy",
  age: 2
cow.color = "Black and White";
cow.gender = "Female";

So it’s up to you. πŸ™‚


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s