Objects are associative arrays with several special features.
Property existence test, “in” operator
There’s also a special operator "in"
"key" in object
let user = { name: "John", age: 30 };
alert( "age" in user ); // true, user.age exists
alert( "blabla" in user ); // false, user.blabla doesn't exist
Order of for in loop
let codes = {
"49": "Germany",
"41": "Switzerland",
"44": "Great Britain",
// ..,
"1": "USA"
};
for (let code in codes) {
alert(code); // 1, 41, 44, 49
} **// Works only with number (sorted)**
in below example , this will print how it should
let codes = {
"+49": "Germany",
"+41": "Switzerland",
"+44": "Great Britain",
// ..,
"+1": "USA"
};
for (let code in codes) {
alert( +code ); // 49, 41, 44, 1 non-numbers
}
There are many other kinds of objects in JavaScript:
Array to store ordered data collections,Date to store the information about the date and time,Error to store the information about an error.