Sobes.tech
Junior — Middle

Can you list ways to use the Object construct in JavaScript?

sobes.tech AI

Answer from AI

In JavaScript, the Object construct is used in several main ways:

  1. Creating objects:
const obj1 = new Object();
const obj2 = {};
  1. Declaring properties and methods:
const obj = {
  name: 'Alice',
  greet() { console.log('Hello'); }
};
  1. Using static Object methods:
  • Object.keys(obj) — returns an array of the object's keys.
  • Object.values(obj) — returns an array of the object's values.
  • Object.assign(target, ...sources) — copies properties from sources to the target object.
  • Object.freeze(obj) — makes the object immutable.
  1. Property checking:
if (Object.hasOwn(obj, 'name')) { /* ... */ }
  1. Setting the prototype:
const proto = { greet() { console.log('Hi'); } };
const obj = Object.create(proto);
obj.greet(); // Hi

Thus, Object is a fundamental building block for working with objects in JavaScript.