Objects
T. Phithakjarukorn
T. Phithakjarukorn
Objects in JavaScript
Objects is one of JavaScript's data types and used to store key & value pairs and more complex values. There are a couple of ways to create Objects. One of them is using the object literal syntax. We can put some properties in to the {...} as "key: value" pairs.
// Object literal syntax
let myCar = {
make: "Honda",
model: "Civic",
yearReleased: 2014,
hasSunroof: false,
}
Another possible way to create an object is by object constructor with the new keyword.
// Object constructor syntax
let myCar = new Object()
myCar.make = "Honda"
myCar.model = "Civic"
myCar.yearReleased = 2014
myCar.hasSunroof = false
To remove a property, we can use delete operator.
delete myCar.model;
If you want to use multiple words in key to name a property, they have to be wrapped in quotes.
let myCar = {
make: "Honda",
model: "Civic",
"year released": 2014,
hasSunroof: false,
}
In order to access the property with multiple words, dot notation would not work. However, square bracket notation has come to the rescue.
myCar.year released
// Uncaught SyntaxError: Unexpected identifier
myCar["year released"]
// output --> 2014
Computed properties
What if you want to wait for a user input to be assigned as key. You can use square bracket for computed properties.
let car = prompt("What is your favorite car?")
let garage = {
[car] : "Awesome",
};
alert(garage.Tesla)