Member-only story
Objects are one of the core pillars upon which JavaScript is built. Understanding objects is key to leveraging the full power and flexibility of the JavaScript language. This article will provide a beginner friendly introduction to using objects in JavaScript.
What are Objects?
Objects in JavaScript are collections of keyed values. The keys refer to properties or methods, while the values can be primitive data types, functions, or even other objects. Here’s a basic example of a JavaScript object:
let person = {
name: "John",
age: 30,
greet: function() {
console.log("Hello!");
}
}
This person object contains the name and age properties to store data values, and the greet method to display a greeting.
Objects are incredibly useful for organizing related data and functionality. Instead of having scattered variables and functions, we can group them together into meaningful objects that represent real-world entities.
Creating Objects
There are a few different ways to create objects in JavaScript: