Learn JavaScript - Objects Cheatsheet - Codecademy
Learn JavaScript - Objects Cheatsheet - Codecademy
Objects
The shorthand property name syntax in JavaScript allows const activity = 'Surfing';
creating objects without explicitly specifying the property
const beach = { activity };
names (ie. explicitly declaring the value after the key). In
this process, an object is created where the property console.log(beach); // { activity:
names of that object match variables which already exist 'Surfing' }
in that context. Shorthand property names populate an
object with a key matching the identifier and a value
matching the identifier’s value.
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-objects/cheatsheet 1/10
1/18/24, 10:43 AM 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 2/10
1/18/24, 10:43 AM 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 3/10
1/18/24, 10:43 AM Learn JavaScript: Objects Cheatsheet | Codecademy
A JavaScript function that returns an object is known as a // A factory function that accepts 'name',
factory function. Factory functions often accept
// 'age', and 'breed' parameters to return
parameters in order to customize the returned object.
// a customized dog object.
const dogFactory = (name, age, breed) => {
return {
name: name,
age: age,
breed: breed,
bark() {
console.log('Woof!');
}
};
};
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-objects/cheatsheet 4/10
1/18/24, 10:43 AM Learn JavaScript: Objects Cheatsheet | Codecademy
JavaScript object key names must adhere to some // Example of invalid key names
restrictions to be valid. Key names must either be strings
const trainSchedule = {
or valid identifier or variable names (i.e. special
characters such as - are not allowed in key names that platform num: 10, // Invalid because of
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 5/10
1/18/24, 10:43 AM 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 6/10
1/18/24, 10:43 AM Learn JavaScript: Objects Cheatsheet | Codecademy
The JavaScript for...in loop can be used to iterate over let mobile = {
the keys of an object. In each iteration, one of the
brand: 'Samsung',
properties from the object is assigned to the variable of
that loop. model: 'Galaxy Note 9'
};
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-objects/cheatsheet 7/10
1/18/24, 10:43 AM 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 8/10
1/18/24, 10:43 AM Learn JavaScript: Objects Cheatsheet | Codecademy
changeItUp(origNum, origObj);
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-objects/cheatsheet 9/10
1/18/24, 10:43 AM 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...
*/
Print Share
https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-objects/cheatsheet 10/10