How to use Object in JavaScript

1 Answer

0 votes
"use strict";

/*
 * Object Example in JavaScript
 * ----------------------------
 * Demonstrates:
 * 1. Creating objects
 * 2. Accessing properties
 * 3. Adding and updating properties
 * 4. Iterating over properties
 * 5. Using Object utility methods
 * 6. Creating reusable functions
 */


/*
Common Object Methods
---------------------
const user = {
    id: 1,
    name: "Luna"
};

// Returns property names
Object.keys(user);

// Returns property values
Object.values(user);

// Returns key/value pairs
Object.entries(user);

// Creates a shallow copy
Object.assign({}, user);

// Prevent modifications
Object.freeze(user);

// Prevent adding/removing properties
Object.seal(user);

// Checks if property belongs directly to object
Object.hasOwn(user, "name");
*/

// Create an object using an object literal (recommended)
const person = {
    firstName: "John",
    lastName: "Doe",
    age: 35,
    isEmployed: true
};

/**
 * Returns a formatted description of a person.
 *
 * @param {Object} personObject - Person object.
 * @returns {string} Formatted description.
 */
function getPersonSummary(personObject) {
    return `${personObject.firstName} ${personObject.lastName} is ${personObject.age} years old.`;
}

// ------------------------------------------------------------------
// Accessing Object Properties
// ------------------------------------------------------------------

console.log("=== Accessing Properties ===");

console.log(person.firstName);      // Dot notation
console.log(person["lastName"]);    // Bracket notation

// ------------------------------------------------------------------
// Updating Properties
// ------------------------------------------------------------------

console.log("\n=== Updating Properties ===");

person.age = 31;

console.log(person);

// ------------------------------------------------------------------
// Adding New Properties
// ------------------------------------------------------------------

console.log("\n=== Adding Properties ===");

person.city = "New York";

console.log(person);

// ------------------------------------------------------------------
// Calling a Function with an Object
// ------------------------------------------------------------------

console.log("\n=== Function Example ===");

console.log(getPersonSummary(person));

// ------------------------------------------------------------------
// Checking Property Existence
// ------------------------------------------------------------------

console.log("\n=== Property Checks ===");

console.log("age" in person);       // true
console.log("salary" in person);    // false

// ------------------------------------------------------------------
// Iterating Through Object Properties
// ------------------------------------------------------------------

console.log("\n=== Iteration ===");

for (const key in person) {
    if (Object.hasOwn(person, key)) {
        console.log(`${key}: ${person[key]}`);
    }
}

// ------------------------------------------------------------------
// Object.keys()
// ------------------------------------------------------------------

console.log("\n=== Object.keys() ===");

const keys = Object.keys(person);

console.log(keys);

// ------------------------------------------------------------------
// Object.values()
// ------------------------------------------------------------------

console.log("\n=== Object.values() ===");

const values = Object.values(person);

console.log(values);

// ------------------------------------------------------------------
// Object.entries()
// ------------------------------------------------------------------

console.log("\n=== Object.entries() ===");

const entries = Object.entries(person);

for (const [key, value] of entries) {
    console.log(`${key}: ${value}`);
}

// ------------------------------------------------------------------
// Cloning an Object
// ------------------------------------------------------------------

console.log("\n=== Cloning ===");

// Create a shallow copy
const copiedPerson = {
    ...person
};

console.log(copiedPerson);

// ------------------------------------------------------------------
// Freezing an Object
// ------------------------------------------------------------------

console.log("\n=== Object.freeze() ===");

const settings = Object.freeze({
    theme: "dark",
    language: "en"
});

console.log(settings);

// settings.theme = "light"; // Ignored or throws in strict mode

// ------------------------------------------------------------------
// Complete Object Output
// ------------------------------------------------------------------

console.log("\n=== Final Objects ===");

console.log("Person:", person);
console.log("Copy:", copiedPerson);
console.log("Settings:", settings);


/*
run:

=== Accessing Properties ===
John
Doe

=== Updating Properties ===
{ firstName: 'John', lastName: 'Doe', age: 31, isEmployed: true }

=== Adding Properties ===
{
  firstName: 'John',
  lastName: 'Doe',
  age: 31,
  isEmployed: true,
  city: 'New York'
}

=== Function Example ===
John Doe is 31 years old.

=== Property Checks ===
true
false

=== Iteration ===
firstName: John
lastName: Doe
age: 31
isEmployed: true
city: New York

=== Object.keys() ===
[ 'firstName', 'lastName', 'age', 'isEmployed', 'city' ]

=== Object.values() ===
[ 'John', 'Doe', 31, true, 'New York' ]

=== Object.entries() ===
firstName: John
lastName: Doe
age: 31
isEmployed: true
city: New York

=== Cloning ===
{
  firstName: 'John',
  lastName: 'Doe',
  age: 31,
  isEmployed: true,
  city: 'New York'
}

=== Object.freeze() ===
{ theme: 'dark', language: 'en' }

=== Final Objects ===
Person: {
  firstName: 'John',
  lastName: 'Doe',
  age: 31,
  isEmployed: true,
  city: 'New York'
}
Copy: {
  firstName: 'John',
  lastName: 'Doe',
  age: 31,
  isEmployed: true,
  city: 'New York'
}
Settings: { theme: 'dark', language: 'en' }

*/

 



answered Oct 16 by avibootz
...