DEV Community

Cover image for Data types in Javascript
Daniel John Keefer
Daniel John Keefer

Posted on

Data types in Javascript

JavaScript has nine fundamental data types, these data types can be categorised as six primitives, two structural and a special primitive-like value called null.


Alt Text

Null

null is a special datatype that for all intents and purposes function like a primitive data type. it is used to represent a void, a empty value. It is a literal representation of nothing within JavaScript. It is special because it is actually an object that it the base for all other objects that are made in JavaScript but it useful to think of it as an absence of data.


Alt Text

Primitives

Primitive data comes in a couple of different flavours and are the most basic building blocks of code in JavaScript. These are the types of data that aren't objects and have no methods. Another key feature is that all primitives are immutable. This means they cannot be changed. It can be tricky to understand this at first because it is easy to think of a variable that is assigned a primitive to be the same as the primitive.

Look at this snippet from MDN that shows a variable with a string assigned doesn't alter when you use string methods on it, but can be reassigned with a new string that is the output of that method. (it also shows that an **Array* that is a structural datatype is mutable. But I'll get to that in a second.*)

// This snippet is from MDN glossary entry for Primitives

// Using a string method doesn't mutate the string
var bar = "baz";
console.log(bar);               // baz
bar.toUpperCase();
console.log(bar);               // baz

// Using an array method mutates the array
var foo = [];
console.log(foo);               // []
foo.push("plugh");
console.log(foo);               // ["plugh"]

// Assignment gives the primitive a new (not a mutated) value
bar = bar.toUpperCase();       // BAZ

Primitive data types include:

Numbers

These are floats and integers between negative and positive (2**53)-1 (+/-9007199254740991) and the special values of NAN, which stands for 'Not A Number', it means somewhere in your arithmetic something other than a number was introduced. And Infinity, which represents a value greater than any number (like the result of something dived by zero, for example.).

BigInt

BigInt is very similar to numbers except instead of having a 64bit precision they are represented with arbitrary-precision, which means the digits in a BigInt are precise to the amount of available memory the local machine can allocate to represent the number. These are used to represent exceedingly large magnitude number in both the positive and negative direction.

Boolean

Booleans are the logical values of true and false used to implement flow control with things like if() statements and loops.

Strings

Strings are a sequence of text characters, not much more to say about that.

Symbols

Symbols are a new primitive introduced with ECMAScript 2015, if you are familiar with Ruby Symbols these are different so don't be fooled. They create an anonymous unique identifier that can optionally given a description.

Undefined

undefined is very similar to the null described earlier. The difference is that null represents an absence of data, where undefined indicated a variable that has been declared but has no data assigned to it.


Alt Text

Structural

Structural types include the Object and the Function

Functions

a function is a non-data structure that responds to typeof keyword. Functions are derived from objects but typically this not necessary to know.

Objects

Finally there are Objects, these are all the data structures that can be made with the new keyword. This list includes: Objects, Arrays, Sets and Maps to name a few, typically these are objects that a constructed by combining multiple primitive data types in some sort of list or some specific format like in a Date.

[Sources]---

  1. Data Types | Ilya Kantor
  2. JavaScript Data Types | W3Schools
  3. JavaScript data types and data structures | MDN
  4. Symbol | MDN

Top comments (0)