JavaScript - Arrays, Loops, and Objects - Objects Cheatsheet - Codecademy
JavaScript - Arrays, Loops, and Objects - Objects Cheatsheet - Codecademy
Objects
Objects
An object is a built-in data type for storing key-value
pairs. Data inside objects are unordered, and the values
can be of any type.
console.log(student)
// { name: 'Sheldon', score: 100, grade:
'A' }
delete student.score
student.grade = 'F'
console.log(student)
// { name: 'Sheldon', grade: 'F' }
student = {}
// TypeError: Assignment to constant
variable.
Delete operator
Once an object is created in JavaScript, it is possible to const person = {
remove properties from the object using the
firstName: "Matilda",
delete operator. The delete keyword deletes
both the value of the property and the property itself age: 27,
from the object. The delete operator only works hobby: "knitting",
on properties, not on variables or functions.
goal: "learning JavaScript"
};
console.log(person);
/*
{
firstName: "Matilda"
age: 27
goal: "learning JavaScript"
}
*/
changeItUp(origNum, origObj);
engine.start('noisily');
engine.sputter();
/* Console output:
The engine starts up noisily...
The engine sputters...
*/
this Keyword
The reserved keyword this refers to a method’s const cat = {
calling object, and it can be used to access properties
name: 'Pipey',
belonging to that object.
Here, using the this keyword inside the object age: 8,
function to refer to the cat object and access its whatName() {
name property. return this.name
}
};
console.log(cat.whatName());
// Output: Pipey