Objects
Learn about JavaScript ES6 object syntax to model real-world items.
StartKey Concepts
Review core concepts you need to learn to master this subject
Restrictions in Naming Properties
Dot Notation for Accessing Object Properties
Objects
Accessing non-existent JavaScript properties
JavaScript Objects are Mutable
JavaScript for...in
loop
Properties and values of a JavaScript object
Delete operator
Restrictions in Naming Properties
Restrictions in Naming Properties
// Example of invalid key names
const trainSchedule = {
platform num: 10, // Invalid because of 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.
}
JavaScript object key names must adhere to some restrictions to be valid. Key names must either be strings or valid identifier or variable names (i.e. special characters such as -
are not allowed in key names that are not strings).
- 1It’s time to learn more about the basic structure that permeates nearly every aspect of JavaScript programming: objects. You’re probably already more comfortable with objects than you think, becau…
- 2Objects can be assigned to variables just like any JavaScript type. We use curly braces, {}, to designate an object literal: let spaceship = {}; // spaceship is an empty object We fill an object…
- 3There are two ways we can access an object’s property. Let’s explore the first way— dot notation, .. You’ve used dot notation to access the properties and methods of built-in objects and dat…
- 4The second way to access a key’s value is by using bracket notation, [ ]. You’ve used bracket notation when indexing an array: [‘A’, ‘B’, ‘C’][0]; // Returns ‘A’ To use bracket notation to access…
- 5Once we’ve defined an object, we’re not stuck with all the properties we wrote. Objects are mutable meaning we can update them after we create them! We can use either dot notation, ., or bracke…
- 7In application code, objects are often nested— an object might have another object as a property which in turn could have a property that’s an array of even more objects! In our spaceship o…
- 8Objects are passed by reference. This means when we pass a variable assigned to an object into a function as an argument, the computer interprets the parameter name as pointing to the space in me…
- 9Loops are programming tools that repeat a block of code until a condition is met. We learned how to iterate through arrays using their numerical indexing, but the key-value pairs in objects aren’t …
What you'll create
Portfolio projects that showcase your new skills
Meal Maker
It's time to build fluency in JavaScript objects. In this next Pro Project, we're going to practice getters and setters in JavaScript so you can hone your skills and feel confident taking them to the real world. Why? Getters and setters allow you to easily insert and retrieve data out of an object. What's next? Eating out, a three course meal, more JavaScript. You got this!
Team Stats
It's time to build fluency in JavaScript Data Structures. In this next Pro Project, we're going to practice Objects in JavaScript so you can hone your skills and feel confident taking them to the real world. Why? Objects are a very important data structure in JavaScript. What's next? Sports stats, data manipulation, more JavaScript. You got this!
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory