4. The 7 Data Types in JavaScript

JavaScript Data Types

There are 7 data-types in JavaScript. 6 of those are "primitives" and the other is the "object" data type. Technically, arrays and functions are just unique implementations of an object, and therefore, are objects.


Primitives

Definitions from MDN Web Docs:

  • Boolean

    Boolean represents a logical entity and can have two values: true, and false.

  • Null

    Meaning, nothing. The Null type has exactly one value: null.

  • Undefined

    A variable that has not been assigned a value has the value undefined.

  • Number

    According to the ECMAScript standard, there is only one number type... There is no specific type for integers. In addition to being able to represent floating-point numbers [decimals], the number type has three symbolic values: +Infinity, -Infinity, and NaN (not-a-number).

  • String

    JavaScript's String type is used to represent textual data. It is a set of "elements" of 16-bit unsigned integer values. Each element in the String occupies a position in the String. The first element is at index 0, the next at index 1, and so on. The length of a String is the number of elements in it.

  • Symbol (new in ECMAScript 6)

    Symbols are new to JavaScript in ECMAScript Edition 6. A Symbol is a unique and immutable primitive value and may be used as the key of an Object property. In some programming languages, Symbols are called atoms.


Exercise

Take some time to look at each data-type above ensuring that you can each define them all in your own words.


Objects

In JavaScript, all other data-types are technically objects (arrays and functions too). An object is essentially just a key-value pair of data.

var car = { color: 'red', wheels: 4 }

If we were to draw a diagram of this, we would see that that the var is referencing the object itself, which is a collection of properties, each referencing a primitive data-type (a string and an number).

object diagram

Now, let's think about the implementation of an array:

var people = ['Aaron', 'Sean'];

If we were to diagram this, we would see a similar pattern, only with the array, our property keys are replaced with sequential indices:

array diagram


Conclusion

Hopefully this is a brief review of concepts you already know! Let's move on to the next part of our overview.

Complete and Continue