Learn JavaScript - Objects Cheatsheet - Codecademy
Learn JavaScript - Objects Cheatsheet - Codecademy
Objects
JavaScript object key names must adhere to some // Example of invalid key names
restrictions to be valid. Key names must either be
const trainSchedule = {
strings or valid identifier or variable names (i.e. special
characters such as - are not allowed in key names
platform num: 10, // Invalid because of
that are not strings). the space between words.
40 - 10 + 2: 30, // Expressions cannot
be keys.
+compartment: 'C' // The use of a +
sign is invalid unless it is enclosed in
quotations.
}
Objects
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-objects/cheatsheet 1/9
15/11/2023 19:12 Learn JavaScript: Objects Cheatsheet | Codecademy
console.log(classElection.place); //
undefined
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.
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-objects/cheatsheet 2/9
15/11/2023 19:12 Learn JavaScript: Objects Cheatsheet | Codecademy
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-objects/cheatsheet 3/9
15/11/2023 19:12 Learn JavaScript: Objects Cheatsheet | Codecademy
Delete operator
console.log(person);
/*
{
firstName: "Matilda"
age: 27
goal: "learning JavaScript"
}
*/
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-objects/cheatsheet 4/9
15/11/2023 19:12 Learn JavaScript: Objects Cheatsheet | Codecademy
changeItUp(origNum, origObj);
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-objects/cheatsheet 5/9
15/11/2023 19:12 Learn JavaScript: Objects Cheatsheet | Codecademy
JavaScript objects may have property values that are const engine = {
functions. These are referred to as object methods.
// method shorthand, with one argument
Methods may be defined using anonymous arrow
function expressions, or with shorthand method syntax.
start(adverb) {
Object methods are invoked with the syntax: console.log(`The engine starts up
objectName.methodName(arguments) . ${adverb}...`);
},
// anonymous arrow function expression
with no arguments
sputter: () => {
console.log('The engine
sputters...');
},
};
engine.start('noisily');
engine.sputter();
/* Console output:
The engine starts up noisily...
The engine sputters...
*/
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-objects/cheatsheet 6/9
15/11/2023 19:12 Learn JavaScript: Objects Cheatsheet | Codecademy
this Keyword
console.log(cat.whatName());
// Output: Pipey
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-objects/cheatsheet 7/9
15/11/2023 19:12 Learn JavaScript: Objects Cheatsheet | Codecademy
JavaScript arrow functions do not have their own this const myObj = {
context, but use the this of the surrounding lexical
data: 'abc',
context. Thus, they are generally a poor choice for
writing object methods.
loggerA: () => {
Consider the example code: console.log(this.data); },
loggerA is a property that uses arrow notation to loggerB() { console.log(this.data);
define the function. Since data does not exist in the
},
global context, accessing this.data returns
undefined .
};
loggerB uses method syntax. Since this refers to the
enclosing object, the value of the data property is myObj.loggerA(); // undefined
accessed as expected, returning "abc" .
myObj.loggerB(); // 'abc'
JavaScript getter and setter methods are helpful in part const myCat = {
because they offer a way to intercept property access
_name: 'Snickers',
and assignment, and allow for additional actions to be
performed before these changes go into effect.
get name(){
return this._name
},
set name(newName){
//Verify that newName is a non-empty
string before setting as name property
if (typeof newName === 'string' &&
newName.length > 0){
this._name = newName;
} else {
console.log("ERROR: name must be a
non-empty string");
}
}
}
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-objects/cheatsheet 8/9
15/11/2023 19:12 Learn JavaScript: Objects Cheatsheet | Codecademy
A JavaScript function that returns an object is known as // A factory function that accepts
a factory function. Factory functions often accept
'name',
parameters in order to customize the returned object.
// 'age', and 'breed' parameters to
return
// a customized dog object.
const dogFactory = (name, age, breed) =>
{
return {
name: name,
age: age,
breed: breed,
bark() {
console.log('Woof!');
}
};
};
Print Share
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-objects/cheatsheet 9/9