cft

Advanced Concepts in Javascript: Types

“Everything in Javascript is an object”


user

Allan Sendagi

2 years ago | 2 min read

All programming languages have types. These are the building blocks that allow us to write in that language. Understanding types is important if our aim is to master the language.

In Javascript, there are only 7 types:

1. Boolean

2. Number

3. String

4. Undefined/absence of a definition

5. Object

6. Symbol

7. object/absence of value

But wait! Notice that null is an object! Well, this is a mistake that comes with the language because even Brendan Eich— the guy who wrote the language acknowledges it. No program is perfect. Because null should be null — an actual primitive type in Javascript but like I said, no program is bug-free.

Symbols() came in ES6. A Symbol value is useful in identifying objects. They are usually used for object properties so that the object property is unique.

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol

Note:

Functions

Technically, functions are objects, underneath it all functions are objects

Let's look at an example to demonstrate.

Functions are objects: Its why we are able to add a property to a function

Arrays

Arrays in Javascript are objects (more on this later).

Arrays and Functions in Javascript are objects.

Primitive and non-primitive types

In Javascript, all types other than the object type are all primitives. Primitive types are data that represent only a single value.

For example:
var a = 5. In memory here, the value is 5 and there is no ambiguity about it.

A variable of a primitive type directly contains a value of that type. null is just null in memory. So is Symbol(), true/false. These can’t be broken down into smaller parts.

A nonprimitive type, on the other hand, doesn't contain an actual value directly.

obj1 doesn't contain the value here directly. Instead, it has a reference — similar to a pointer to somewhere in memory that object is held.

Built-in objects

Standard built-in objects come with the language.

However, notice that we also have Strings, Booleans, Numbers for example, even though we said that these are primitives. How come they are objects?

This gets a little complicated.

But “everything in Javascript is an object”. Many things we interact with directly such as strings, numbers, and booleans which are primitives have object wrappers like string(), Boolean() or Number().

For example

true is a primitive type but why does it act as an object by using the dot notation?

Again this is how weird Javascript gets sometimes. Because it silently creates a wrapper object around true when we try and attempt to access a property on a primitive.

Behind the scenes, it's almost like wrapping this in a Boolean so that it has access toString() and then finally returns “true”.

So its good to remember that things like Booleans or Strings exist in order for us to be able to use methods on the primitives values.

So not everything in Javascript is an object. But there are a lot of built-in objects that we can use so that if we do typeof Math,

we get an object. And we do typeof infinity,

we get a number.

Upvote


user
Created by

Allan Sendagi

Technology will save homo sapiens from extinction


people
Post

Upvote

Downvote

Comment

Bookmark

Share


Related Articles