Skip to content

Update article.md to expand on property order in objects #3368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion 1-js/04-object-basics/01-object/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ Also, we could use another variable name here instead of `key`. For instance, `"

Are objects ordered? In other words, if we loop over an object, do we get all properties in the same order they were added? Can we rely on this?

The short answer is: "ordered in a special fashion": integer properties are sorted, others appear in creation order. The details follow.
The short answer is: "ordered in a special fashion": Integer properties appear first in sorted order, then other properties appear in creation order. The details follow.

As an example, let's consider an object with the phone codes:

Expand Down Expand Up @@ -472,6 +472,21 @@ for (let code in codes) {

Now it works as intended.

If an object has both integer properties and non-integer properties, the integer properties will appear first, regardless of creation order.

```js run
let favoriteGamesTopThree = {};
favoriteGamesTopThree.year = 1996;
favoriteGamesTopThree.author = "John Smith";
favoriteGamesTopThree[3] = "Crash Bandicoot";
favoriteGamesTopThree[2] = "Super Mario 64";
favoriteGamesTopThree[1] = "Quake";

for (let prop in favoriteGamesTopThree) {
alert( prop ); // 1, 2, 3, year, author
}
```

## Summary

Objects are associative arrays with several special features.
Expand Down