How is almost everything in Javascript an object?

Solution 1:

No, not everything is an object in JavaScript. Many things that you interact with regularly (strings, numbers, booleans) are primitives, not objects. Unlike objects, primitive values are immutable. The situation is complicated by the fact that these primitives do have object wrappers (String, Number and Boolean); these objects have methods and properties while the primitives do not, but the primitives appear to have methods because JavaScript silently creates a wrapper object when code attempts to access any property of a primitive.

For example, consider the following code:

var s = "foo";
var sub = s.substring(1, 2); // sub is now the string "o"

Behind the scenes, s.substring(1, 2) behaves as if it is performing the following (approximate) steps:

  1. Create a wrapper String object from s, equivalent to using new String(s)
  2. Call the substring() method with the appropriate parameters on the String object returned by step 1
  3. Dispose of the String object
  4. Return the string (primitive) from step 2.

A consequence of this is that while it looks as though you can assign properties to primitives, it is pointless because you cannot retrieve them:

var s = "foo";
s.bar = "cheese";
alert(s.bar); // undefined

This happens because the property is effectively defined on a String object that is immediately discarded.

Numbers and Booleans also behave this way. Functions, however, are fully-fledged objects, and inherit from Object (actually Object.prototype, but that's another topic). Functions therefore can do anything objects can, including having properties:

function foo() {}
foo.bar = "tea";
alert(foo.bar); // tea

Solution 2:

That’s right: in JavaScript, almost everything is an object. But these objects are bit different from what we see in Java, C++ or other conventional languages. An object in JS is simply a hashmap with key–value pairs. A key is always a string or a symbol, and a value can be anything including strings, integers, booleans, functions, other objects etc. So I can create a new object like this:

var obj = {}; // This is not the only way to create an object in JS

and add new key–value pairs into it:

obj['message'] = 'Hello'; // You can always attach new properties to an object externally

or

obj.message = 'Hello';

Similarly, if I want to add a new function to this object:

obj['showMessage'] = function(){
  alert(this['message']);
}

or

obj.showMessage = function() {
  alert(this.message);
}

Now, whenever I call this function, it will show a pop-up with a message:

obj.showMessage();

Arrays are simply those objects which are capable of containing lists of values:

var arr = [32, 33, 34, 35]; // One way of creating arrays in JS

Although you can always use any object to store values, but arrays allow you to store them without associating a key with each of them. So you can access an item using its index:

alert(arr[1]); // This would show 33

An array object, just like any other object in JS, has its properties, such as:

alert(arr.length); // This would show 4

For in-depth detail, I would highly recommend John Resig’s Pro JavaScript Techniques.