Leran JavaScripts

Get started with JS Objects

JavaScript Object Definition

Methods for Defining JavaScript Objects
Using::
an Object Literal
the new Keyword
an Object Constructor
Object.assign()
Object.create()
Object.fromEntries()


JavaScript Object Literal

An object literal is a list of property names:values inside curly braces {}.


{firstName:"John", lastName:"Doe", age:28};
              
Creating a JavaScript Object
Examples:
Create an empty JavaScript object using {}, and add properties:

// Create an Object
const person = {};

// Add Properties
person.firstName = "John";
person.lastName = "Doe";
person.age = 28;
              
Create an empty JavaScript object using new Object(), and add properties:

// Create an Object
const person = new Object();

// Add Properties
person.firstName = "John";
person.lastName = "Doe";
person.age = 28;
              
Object Constructor Functions
To create an object type we use an object constructor function.
Object Type Person
       
function Person(first, last, age) {
  this.firstName = first;
  this.lastName = last;
  this.age = age;
}