Javascript Objects

Javascript Objects

In JavaScript, an object is an unordered collection of data. It can be defined as a variable that can hold many values. Objects are created using curly brackets {} with an optional list of properties. Properties are a key-value pair, where the key is a string (also called a property name), and the value can be of any data type.

Creating an Object in JavaScript:

let person = {
    firstName: "John",
    lastName: "Doe",
    age: 25,
    hobbies: ['reading', 'swimming', 'traveling']
};

Accessing Object Properties:

console.log(person.firstName); // output: John
console.log(person['lastName']); // output: Doe

JavaScript provides various built-in methods to manipulate objects. Some of the most commonly used object methods are:

  1. Object.assign(): The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It takes two or more objects as arguments and returns a new object that contains the merged properties.

Example:

const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };
const result = Object.assign(target, source);
console.log(result); //output: {a: 1, b: 4, c: 5}
  1. Object.keys(): The Object.keys() method is used to return an array of a given object's own enumerable property names, in the same order as we get with a normal loop.

Example:

const person = { firstName: "John", lastName: "Doe", age: 25 };
console.log(Object.keys(person)); //output: ["firstName", "lastName", "age"]
  1. Object.values(): The Object.values() method is used to return an array of a given object's own enumerable property values, in the same order as we get with a normal loop.

Example:

const person = { firstName: "John", lastName: "Doe", age: 25 };
console.log(Object.values(person)); //output: ["John", "Doe", 25]
  1. Object.entries(): The Object.entries() method is used to return an array of a given object's own enumerable property [key, value] pairs, in the same order as we get with a normal loop.

Example:

const person = { firstName: "John", lastName: "Doe", age: 25 };
console.log(Object.entries(person)); //output: [["firstName", "John"], ["lastName", "Doe"], ["age", 25]]
  1. Object.freeze(): The Object.freeze() method is used to freeze an object. Once an object is frozen, you cannot add, delete, or modify its properties.

Example:

const person = { firstName: "John", lastName: "Doe", age: 25 };
Object.freeze(person);
person.age = 30;
console.log(person.age); //output: 25
  1. Object.seal(): The Object.seal() method is used to seal an object. Once an object is sealed, you cannot add or delete its properties, but you can modify the existing properties.

Example:

const person = { firstName: "John", lastName: "Doe", age: 25 };
Object.seal(person);
person.age = 30;
console.log(person.age); //output: 30
person.gender = "Male";
console.log(person.gender); //output: undefined

In conclusion, JavaScript objects are versatile and powerful, and they can be manipulated in various ways using the built-in