Objects in JavaScript

An object in JavaScript (and perhaps in all other programming languages), is a collection of key-value pairs. Where key is the identifier and value is the value for that key.

A simple object:

var person = {
	'firstName': 'Michael',
	'lastName': 'Scott'

There are other ways of creating an object, but this is the simplest version. This is also called the object literal notation.

Getting the value of from an object: [Getting value from object is same, irrespective of how the object was created]

// Method 1
console.log (person.firstName);    // "Michael"
console.log (person.lastName);     // "Scott"

// Method 2
console.log (person['firstName']);    // "Michael"
console.log (person['lastName']);    // "Michael"

In general, while creating an object, we should create keys of a plain string. But sometimes, for whatever reason, we might end up having keys with special characters in it. Out of those special characters, hyphen (-) is one which cannot be accessed using method 1.

// invalid key
var person = {
  first-name: 'Jim'
// result => invalid token -

// valid key, with quotes around the key
var person = {
  'first-name': 'Jim'

// accessing
console.log (person.first-name);    // invalid token -
console.log (person['first-name');  // "Jim"

As an object is a collection of key-value pairs, a value can be of any type. The value for a key could be an object, array, function, or any thing else.

var person = {
  name: {
    first: 'Dwight',
    last: 'Schrute'
  location: 'Scranton',
  getDetail: function () {
    return this.name.first + " " + this.name.last + " works in " + this.location;

Everything that is not a primitive data type is an object in JavaScript. An array is an object, a function is an object, and an object is an object. To be precise, functions and arrays are instances of Function and Array objects respectively. Also, if we go back to the primitives, even strings, numbers, and booleans are also instances of String, Number, and Boolean objects.

Before we go further, let’s see how primitive string and string object are different.

Consider following examples:
var oStr = new String ("O Str");
var str = "test";

typeof oStr; // "object"
typeof str;  // "string"

JavaScript implicitly converts all the primitive strings to the String object. The question is, is that something JavaScript must do?

Consider this, whenever we create a string (primitive), we are able to use all the methods, like substring, split, charCode, etc. But a primitive data type is just a value, then how do we do this? The answer is, because JavaScript implicitly converts them to the String Object, and String Object has all those methods

// same example as above
// even though typeof str is a string, we can still check the
// length attribute, as well as other string methods with str

oStr.length;        // 5
str.length;         // 4

Another difference between String object and primitive string is, how do they respond when passed to eval:

var adder = '2 + 2';
var oAdder = new String ('2 + 2');

eval (adder);        // 4
eval (oAdder);       // "2 + 2"

// run it in a console to view detailed information returned
// String object

Discussing [all] the methods of String or Array is beyond the scope of this post.

For a detailed explanation on how can, we create objects read this post: JavaScript Constructor Pattern | Objects in JavaScript