creating objects in javascript
Explore our JavaScript category, where you’ll find loads of helpful articles and tutorials for everyone, whether you’re just starting out or a pro developer. Learn about different JavaScript functions that can be used in various projects according to their specific requirements.
1- Object Initialization:
<script>
var itemDress = {
color: "Red",
size: "small",
price: "20"
};
</script>
var itemDress = {
color: "Red",
size: "small",
price: "20"
};
</script>
This creates an object itemDress with three properties: color, size, and price, and initializes them with the values “Red,” “small,” and “20,” respectively.
2- Logging Initial State:
console.log(itemDress);
This line logs the initial state of the itemDress object to the console.
3- Price Update:
itemDress.price = "30";
This line updates the price property of the itemDress object to “30.”
4- Logging Updated State:
console.log(itemDress);
This line logs the updated state of the itemDress object to the console after the price has been modified.
<script>
var itemDress = {
color: "Red",
size: "small",
price: "20"
}
console.log(itemDress);
itemDress.price = "30";
console.log(itemDress);
</script>
var itemDress = {
color: "Red",
size: "small",
price: "20"
}
console.log(itemDress);
itemDress.price = "30";
console.log(itemDress);
</script>
When you run this script, you’ll see two sets of console logs—one for the initial state and one for the updated state of the itemDress object. The initial state will show the price as “20,” and the updated state will show the price as “30” after the modification.