Create
An object can be created with figure brackets {...} with an optional list of properties.
let A = {};
let B = {
name: "John",
age: 30,
};
let x = "mykey";
let C = {
"some key": 1,
[x]: 3,
}
console.log(A.something);
console.log(B.name);
console.log(C['mykey'])
Shorthand
It is very common to make a property from a variable.
function makeUser(name, age) {
return {
name,
age,
tasks: 10,
};
}
let user = makeUser("Marry", 18);
console.log(user.age);
console.log(user.tasks);
For ... in
In Javascript you can access any properties, even if doesn't exists.
let A = {
'name': 'John',
'age': 30,
};
for (key in A) {
console.log(C[key]);
}
console.log('xyz' in A);