JavaScript Objects and JSON
JavaScript Objects
Key-value collections for structured data.
Object Examples
let person = {
name: "John",
age: 25,
greet() {
return "Hi, I'm " + this.name;
}
};
console.log(person.name); // "John"
console.log(person.greet()); // "Hi, I'm John"
// JSON
let json = JSON.stringify(person);
let obj = JSON.parse(json);Key Points
- Dot notation: person.name, bracket: person["name"].
- this refers to current object.
- ES6 shorthand: name instead of name: name.
- JSON for data interchange.