JavaScript Objects in Detail

JavaScript’s core—most often used and most fundamental—data type is the Object data type. JavaScript has one complex data type, the Object data type, and it has five simple data types: Number, String, Boolean, Undefined, and Null. Note that these simple (primitive) data types are immutable (cannot be changed), while objects are mutable (can be changed).

[sc:mongodb-book]

What is an Object
An object is an unordered list of primitive data types (and sometimes reference data types) that is stored as a series of name-value pairs. Each item in the list is called a property (functions are called methods).

Consider this simple object:

var myFirstObject = {firstName: “Richard”, favoriteAuthor: “Conrad”};

Think of an object as a list that contains items, and each item (a property or a method) in the list is stored by a name-value pair. The property names in the example above are firstName and favoriteAuthor. And the values are “Richard” and “Conrad.”

Property names can be a string or a number, but if the property name is a number, it has to be accessed with the bracket notation. More on bracket notation later. Here is another example of objects with numbers as the property name:

var ageGroup = {30: “Children”, 100:”Very Old”};
console.log(ageGroup.30) // This will throw an error
// This is how you will access the value of the property 30, to get value “Children”
console.log(ageGroup[“30”]); … Continue reading JavaScript Objects in Detail