Javascript Object And Methods

Table of contents

No heading

No headings in the article.

JavaScript objects are a powerful tool for organizing and manipulating data in a structured way. They are similar to real-world objects in that they have properties and methods that can be used to access and manipulate those properties. They allow developers to represent data in a meaningful and organized way, making it easy to understand and work with. In other words, JavaScript objects are a fundamental building block of the language, allowing developers to store and manipulate data logically and intuitively.

For example, let's consider the object of a "Person", which can have properties like "name", "age", "address" and "gender".

let person = {
    name: "John Doe",
    age: 35,
    address: "123 Main St.",
    gender: "male"
};

Just like how a person has a name, age, address and gender, this object has the same properties. We can access the properties of the object using the dot notation (objectName.propertyName) or bracket notation (objectName["propertyName"]).

console.log(person.name); // Output: "John Doe"
console.log(person["age"]); // Output: 35

We'll take a look at some of the most commonly used methods in JavaScript objects, along with code examples and their outputs.

  1. Constructor Method: The constructor method is used to create and initialize an object, and it is called automatically when an object is created. The constructor method is defined using the "constructor" keyword, and it can take in any number of parameters. For example:
function Person(name, age) {
    this.name = name;
    this.age = age;
}

let person = new Person("John", 30);
console.log(person.name); // Output: "John"
console.log(person.age); // Output: 30
  1. hasOwnProperty Method: The "hasOwnProperty" method is used to determine if an object has a specific property. It returns a Boolean value of true if the property exists and false if it does not. This method is useful for checking if an object has certain properties before performing actions on them. For example:
let obj = {
    name: "John",
    age: 30
};

console.log(obj.hasOwnProperty("name")); // Output: true
console.log(obj.hasOwnProperty("gender")); // Output: false
  1. forEach Method: The "forEach" method is used to iterate over the properties of an object and perform a function on each one. It takes in a callback function that is called for each property, and it is passed three arguments: the value of the property, the key of the property, and the object itself. For example:
let obj = {
    name: "John",
    age: 30,
    address: "New York"
};

obj.forEach(function(value, key, object) {
    console.log(key + ": " + value);
});

// Output:
// name: John
// age: 30
// address: New York
  1. filter Method: The "filter" method can be used to filter out properties based on certain conditions. It takes in a callback function that is called for each property, and it is passed three arguments: the value of the property, the key of the property, and the object itself. For example:
let obj = {
    name: "John",
    age: 30,
    address: "New York",
    salary: 50000
};

let filteredObj = obj.filter(function(value, key, object) {
    return value > 25;
});
console.log(filteredObj);
// Output: { age: 30, salary: 50000 }

These are some of the most commonly used methods in JavaScript objects. Understanding and utilizing these methods can make working with objects in JavaScript a lot easier and more efficient. There are many more methods available in JavaScript objects, and it's worth exploring them to discover their full potential.

Happy Learning😊