Objects

FatimaAlam1234 - Dec 1 '23 - - Dev Community

Literals and properties
We can immediately put some properties into {...} as “key: value” pairs:

let user = {     // an object
  name: "John",  // by key "name" store value "John"
  age: 30        // by key "age" store value 30
};

/

Enter fullscreen mode Exit fullscreen mode

/ get property values of the object:
alert( user.name ); // John
alert( user.age ); // 30


Enter fullscreen mode Exit fullscreen mode

user.isAdmin = true;

delete user.age;
Enter fullscreen mode Exit fullscreen mode

Square brackets

For multiword properties, the dot access doesn’t work:
/

/ this would give a syntax error
user.likes birds = true
Enter fullscreen mode Exit fullscreen mode

Instead ->

let user = {};
// set
user["likes birds"] = true;
// get
alert(user["likes birds"]); // true
// delete
delete user["likes birds"];
Enter fullscreen mode Exit fullscreen mode

Computed properties

We can use square brackets in an object literal, when creating an object. That’s called computed properties.

let fruit = prompt("Which fruit to buy?", "apple");

let bag = {
  [fruit]: 5, // the name of the property is taken from the variable fruit
};

alert( bag.apple ); // 5 if fruit="apple"
Enter fullscreen mode Exit fullscreen mode

Property value shorthand

function makeUser(name, age) {
  return {
    name: name,
    age: age,
    // ...other properties
  };
}

let user = makeUser("John", 30);
alert(user.name); // John
Enter fullscreen mode Exit fullscreen mode

Instead of name:name we can just write name, like this:

function makeUser(name, age) {
  return {
    name, // same as name: name
    age,  // same as age: age
    // ...
  };
}
Enter fullscreen mode Exit fullscreen mode

We can use both normal properties and shorthands in the same object:

let user = {
  name,  // same as name:name
  age: 30
};
Enter fullscreen mode Exit fullscreen mode

Property names limitations

As we already know, a variable cannot have a name equal to one of the language-reserved words like “for”, “let”, “return” etc.

But for an object property, there’s no such restriction:

// these properties are all right
let obj = {
  for: 1,
  let: 2,
  return: 3
};

alert( obj.for + obj.let + obj.return );  // 6
Enter fullscreen mode Exit fullscreen mode

Other types are automatically converted to strings.

For instance, a number 0 becomes a string "0" when used as a property key:

let obj = {
  0: "test" // same as "0": "test"
};

// both alerts access the same property (the number 0 is converted to string "0")
alert( obj["0"] ); // test
alert( obj[0] ); // test (same property)
Enter fullscreen mode Exit fullscreen mode

There’s a minor gotcha with a special property named proto. We can’t set it to a non-object value:

let obj = {};
obj.__proto__ = 5; // assign a number
alert(obj.__proto__); // [object Object] - the value is an object, didn't work as 
Enter fullscreen mode Exit fullscreen mode

Property existence test, “in” operator
It’s possible to access any property. There will be no error if the property doesn’t exist!

Reading a non-existing property just returns undefined.
There’s also a special operator "in" for that.

The syntax is:

"key" in object
Enter fullscreen mode Exit fullscreen mode

To traverse all keys in the object

let user = {
  name: "John",
  age: 30,
  isAdmin: true
};

for (let key in user) {
  // keys
  alert( key );  // name, age, isAdmin
  // values for the keys
  alert( user[key] ); // John, 30, true
}
Enter fullscreen mode Exit fullscreen mode

Ordered like an object

Integer properties are sorted, others appear in creation order.

let codes = {
  "49": "Germany",
  "41": "Switzerland",
  "44": "Great Britain",
  // ..,
  "1": "USA"
};

for (let code in codes) {
  alert(code); // 1, 41, 44, 49
}
Enter fullscreen mode Exit fullscreen mode

Objects can also have functions as their elements

const Obj = {
k1: v1,
k2: v2,
...
.
.
key = function(value){
return val*!0;
}
};
Enter fullscreen mode Exit fullscreen mode

Only when the function instead of being present as function normal definition it is present as a key-value pair.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .