Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Creating Objects | Understanding and Working with Objects
Objects and Prototypes in JavaScript

bookCreating Objects

Objects are a core feature in JavaScript, used to group related data and functions together. An object is a collection of properties, where each property is a key-value pair.

Use an object literal, which lets you define properties and their values in a single, concise statement.

const obj = {
  property: value,
}
12345678910
const user = { name: "Alice", age: 30, contact: { email: "alice@example.com", phone: "555-1234" } }; console.log(JSON.stringify(user));
copy

Another way to create objects in JavaScript is by using the Object constructor. The Object constructor is a built-in function that can create new objects at runtime. Unlike object literals, which define the object's structure and values in one step, the Object constructor creates a blank object, and then you add properties to it one by one. Referring to the user object literal above, you would need to manually add each property if you use the Object constructor.

12345678
const user2 = new Object(); user2.name = "Alice"; user2.age = 30; user2.contact = {}; user2.contact.email = "alice@example.com"; user2.contact.phone = "555-1234"; console.log(JSON.stringify(user2));
copy

Choosing between object literals and the Object constructor depends on your needs. Object literals are preferred for their simplicity and readability, especially when you know the structure and values ahead of time. The Object constructor can be useful if you need to create objects dynamically or if you are working in situations where object creation is part of a more complex process. In most cases, use object literals for clarity and brevity, while the Object constructor is reserved for scenarios that require dynamic property assignment or when integrating with frameworks that expect constructor-based object creation.

question mark

Which statements about creating objects in JavaScript are correct?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 2

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Awesome!

Completion rate improved to 7.69

bookCreating Objects

Swipe um das Menü anzuzeigen

Objects are a core feature in JavaScript, used to group related data and functions together. An object is a collection of properties, where each property is a key-value pair.

Use an object literal, which lets you define properties and their values in a single, concise statement.

const obj = {
  property: value,
}
12345678910
const user = { name: "Alice", age: 30, contact: { email: "alice@example.com", phone: "555-1234" } }; console.log(JSON.stringify(user));
copy

Another way to create objects in JavaScript is by using the Object constructor. The Object constructor is a built-in function that can create new objects at runtime. Unlike object literals, which define the object's structure and values in one step, the Object constructor creates a blank object, and then you add properties to it one by one. Referring to the user object literal above, you would need to manually add each property if you use the Object constructor.

12345678
const user2 = new Object(); user2.name = "Alice"; user2.age = 30; user2.contact = {}; user2.contact.email = "alice@example.com"; user2.contact.phone = "555-1234"; console.log(JSON.stringify(user2));
copy

Choosing between object literals and the Object constructor depends on your needs. Object literals are preferred for their simplicity and readability, especially when you know the structure and values ahead of time. The Object constructor can be useful if you need to create objects dynamically or if you are working in situations where object creation is part of a more complex process. In most cases, use object literals for clarity and brevity, while the Object constructor is reserved for scenarios that require dynamic property assignment or when integrating with frameworks that expect constructor-based object creation.

question mark

Which statements about creating objects in JavaScript are correct?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 2
some-alt