Eric Sjöström Jennerstrand

Litium E-commerce, C#, .NET MAUI, Javascript, React, Azure, Git. .NET, Node and more!
JavaScript logo

Creating JavaScript Objects: Code Examples

JavaScript is an object-oriented programming language, which means that it uses objects to store data. JavaScript objects are created using the object literal notation, which is a set of curly braces enclosing key-value pairs. Understanding how to create objects is an essential skill for any JavaScript developer, so in this article, we will explore the topic of creating JavaScript objects and provide some code examples.

Understanding JavaScript Objects

In JavaScript, objects are containers for named values, called properties or methods. Objects can be created with the object literal notation, as mentioned before, or with the object constructor function. The properties of an object can be accessed with dot notation or bracket notation.

For example, let’s say we want to create an object called “person” with two properties: “name” and “age”. We can do it with the following code:

let person = {
  name: "John",
  age: 30

To access the properties of the “person” object, we can use dot notation or bracket notation:

console.log(; // "John"
console.log(person["age"]); // 30

Examples of Creating Objects in JavaScript

Let’s explore some more examples of creating objects in JavaScript.

Using the Object Constructor Function

We can also create objects using the Object constructor function. This method is less common than the object literal notation, but it can be useful in some cases. Here’s an example:

let person = new Object(); = "John";
person.age = 30;

Using Object.create()

Another way to create objects in JavaScript is by using the Object.create() method. This method creates a new object with the specified prototype object and properties. Here’s an example:

let personProto = {
  greet: function() {
    console.log("Hello, my name is " +;

let person = Object.create(personProto); = "John";
person.age = 30;

Using Classes

Finally, we can create objects in JavaScript using classes, which were introduced in ECMAScript 6. Classes are a more formal way of defining objects in JavaScript, and they are similar to classes in other programming languages. Here’s an example:

class Person {
  constructor(name, age) { = name;
    this.age = age;

  greet() {
    console.log("Hello, my name is " +;

let person = new Person("John", 30);

Creating objects in JavaScript is an essential skill for any JavaScript developer. In this article, we explored the topic of creating JavaScript objects and provided some code examples. We learned that objects can be created with the object literal notation, the object constructor function, Object.create(), and classes. By understanding how to create objects in JavaScript, you can build more sophisticated and dynamic web applications.

Leave a Reply

Your email address will not be published. Required fields are marked *.

You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>