DEV Community

Harshit Kedia
Harshit Kedia

Posted on

Useful javascript nuggets

Revised javascript objects, noted some useful points:

We can omit the quotes for single-word string properties, like make in the following example-
const anotherObject = {
make: "Ford",
5: "five",
"model": "focus"
};
*Notice 5, we can also use numbers as properties.

If the property of the object we are trying to access has a space in its name, we need to use bracket notation, ex: myObj["Space Name"];

We can add new properties to existing JavaScript objects the same way you would modify them. Ex: ourDog.bark = "bow-wow"; // bark property was not declared initially

delete properties from objects like this: delete ourDog.bark;

someObject.hasOwnProperty(someProperty) returns true or false depending on if the property is found on the object or not

*In a code, records[id][prop].push(value) was giving error because the array was not defined in some cases, so changed the code to:
const arr = records[id][prop] || []
arr.push(value)
records[id][prop]=arr

Top comments (0)