curriculum/challenges/english/blocks/review-javascript-objects/6723c77d1a44b8a7599277dd.md
:::interactive_editor
const person = {
name: "Alice",
age: 30,
city: "New York"
};
console.log(person.name); // Alice
console.log(person["name"]); // Alice
:::
To set a property of an existing object you can use either dot notation or bracket notation together with the assignment operator.
:::interactive_editor
const person = {
name: "Alice",
age: 30
};
person.job = "Engineer"
person["hobby"] = "Knitting"
console.log(person); // {name: 'Alice', age: 30, job: 'Engineer', hobby: 'Knitting'}
:::
delete Operator: This operator is used to remove a property from an object.:::interactive_editor
const person = {
name: "Alice",
age: 30,
job: "Engineer"
};
delete person.job;
console.log(person.job); // undefined
:::
hasOwnProperty() Method: This method returns a boolean indicating whether the object has the specified property as its own property.:::interactive_editor
const person = {
name: "Alice",
age: 30
};
console.log(person.hasOwnProperty("name")); // true
console.log(person.hasOwnProperty("job")); // false
:::
Object.hasOwn() Method: This is the modern, recommended way to check if an object has a property as its own (not inherited). The syntax is Object.hasOwn(object, propertyName). It returns true if the property exists on the object and false if it does not — regardless of the property's value. This makes it safer than hasOwnProperty() and more reliable than if (obj.prop) when values can be 0, false, null, or undefined.:::interactive_editor
const person = {
name: "Alice",
age: 30
};
console.log(Object.hasOwn(person, "name")); // true
console.log(Object.hasOwn(person, "job")); // false
:::
Here is a more detailed example showing why Object.hasOwn() is more reliable than checking the value directly:
:::interactive_editor
const settings = {
darkMode: false,
fontSize: 0,
language: null
};
// Object.hasOwn() correctly sees these properties exist
console.log(Object.hasOwn(settings, "darkMode")); // true (value is false, but exists)
console.log(Object.hasOwn(settings, "fontSize")); // true (value is 0, but exists)
console.log(Object.hasOwn(settings, "theme")); // false (property was never added)
// Using if() directly is unsafe for falsy values!
if (settings.darkMode) {
console.log("Dark mode on"); // Does NOT print — misleading!
}
// Object.hasOwn() is the safe way
if (Object.hasOwn(settings, "darkMode")) {
console.log("darkMode exists, value is:", settings.darkMode); // darkMode exists, value is: false
}
:::
in Operator: This operator will return true if the property exists in the object.:::interactive_editor
const person = {
name: "Bob",
age: 25
};
console.log("name" in person); // true
:::
:::interactive_editor
const person = {
name: "Alice",
age: 30,
contact: {
email: "[email protected]",
phone: {
home: "123-456-7890",
work: "098-765-4321"
}
}
};
console.log(person.contact.phone.work); // "098-765-4321"
:::
null, undefined, and symbols. These types are called "primitive" because they represent single values and are not objects. Primitive values are immutable, which means once they are created, their value cannot be changed.this keyword inside the method refers to the object itself, enabling access to its properties.:::interactive_editor
const person = {
name: "Bob",
age: 30,
sayHello: function() {
return "Hello, my name is " + this.name;
}
};
console.log(person.sayHello()); // "Hello, my name is Bob"
:::
new keyword and can initialize properties and methods on the newly created object. The Object() constructor creates a new empty object.new Object()
?.):::interactive_editor
const user = {
name: "John",
profile: {
email: "[email protected]",
address: {
street: "123 Main St",
city: "Somewhere"
}
}
};
console.log(user.profile?.address?.street); // "123 Main St"
console.log(user.profile?.phone?.number); // undefined
:::
:::interactive_editor
const person = { name: "Alice", age: 30, city: "New York" };
const { name, age } = person;
console.log(name); // Alice
console.log(age); // 30
:::
{
"name": "Alice",
"age": 30,
"isStudent": false,
"list of courses": ["Mathematics", "Physics", "Computer Science"]
}
JSON.stringify(): This method is used to convert a JavaScript object into a JSON string. This is useful when you want to store or transmit data in a format that can be easily shared or transferred between systems.:::interactive_editor
const user = {
name: "John",
age: 30,
isAdmin: true
};
const jsonString = JSON.stringify(user);
console.log(jsonString); // '{"name":"John","age":30,"isAdmin":true}'
:::
JSON.parse(): This method converts a JSON string back into a JavaScript object. This is useful when you retrieve JSON data from a web server or localStorage and you need to manipulate the data in your application.:::interactive_editor
const jsonString = '{"name":"John","age":30,"isAdmin":true}';
const userObject = JSON.parse(jsonString);
// result: { name: 'John', age: 30, isAdmin: true }
console.log(userObject);
:::
Review the JavaScript Objects topics and concepts.