Primitives vs Objects

Topics

1
Primitives vs Objects
2
Properties
3
Type Casting
4
Structures
5
Representations
Continue Later

Chances are, you'll hear a phrase "Everything in JS is an object" and this phrase is incredibly wrong. At first, by "everything" they mean "values" and there are many things that are not values in JS code (e.g. statements). At second, not every value in JavaScript is an object. It's true for Ruby, but not JavaScript.

All values in this language are divided in two categories: primitives and objects. We can also talk about primitive and object types. Two groups are not perfectly comparable (like apples and oranges). For example, objects are first-class citizen and primitives are not. Among other things, that means objects are open to extension (you can create and modify object types) which is not true for primitives (you can only use those predefined).

You can also say that objects look relatively the same to compiler while primitives are different and prove it with the typeof operator. The primary goal of having primitives is optimization (Ruby is one of the slowest languages for a reason). It's still unfortunate that we can't create new primitive types. The consequent lack of enumerations lead to many design flaws in your (and mine) programs.

The set of differences between primitives and objects and the set of favorite interview questions have a lot in common ;) Mutability and immutability, properties and methods, prototypes and delegation – a lot of things stem from or are related to this fundamental distinction. We are not going to replace a textbook for you so let's simply agree on the idea that this topic is crucial and you won't skip its theory.

For the sake of simplicity and brevity we're going to follow other authors and use the word type interchangeably: for both object and primitive types. One could argue it would be more precise to use two different words (e.g types and prototypes) to emphasize the inherent difference between two categories. Still prototypes is not only the one way of looking at objects, there are others like structural typing and this goes too far for our intents and purposes here.

Topics

1
Primitives vs Objects
2
Properties
3
Type Casting
4
Structures
5
Representations
Continue Later