JavaScript primitive data types
Tags: javascript, beginers, data, types
Published: 2021-11-01 13:50:19

---
title: JavaScript primitive data types
published: true
description: Primitive data types in JavaScript
tags: Javascript, beginers, data, types
//cover_image: https://direct_url_to_image.jpg
---

Every programming language has data types. In JavaScript, there are 8 data types that can be grouped into two categories: primitive values and objects.

Primitive values are data types that are immutable (can't be changed). If you come from another programming language like C, this might be something new to you.

For example, in C you learned that string is an array of chars, and you could change each char. Code below in C would change string to "boop", but in JavaScript stays "loop" because the string is a primitive value that can not be changed.

![comparison with c](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/st8dwrgduin29a1jefoc.png)
 
Here is the list of all primitive values:

* Boolean
* Null
* Undefined
* Number
* BigInt
* String
* Symbol

Everything that doesn't have type one of the primitive values is type of object. For example, common mistake is checking type of date, which doesn't exist and is always false. 

![type of date](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k7868bxn2ntwlgx3dikn.png)
 
As you can see, date is actually type of object.

### Boolean

Boolean is a logical data type that can have two values: `true` or `false`. Its mostly used in if statements and loops to conditionally execute code based on truthness of an expression.

![boolean types](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ygrpgr6bv0ko7mah8qa.png)
 
Note that expressions are also type of boolean (2>3) and can be true or false.

Also true and false aren't same as "true" and "false", later ones are type of string.

### Null and undefined

Null represents the intentional absence of object value. If you check it's type you will notice it actually isn't type of null, but actually type of object. Its used when you expect an object returned, but instead you get returned nothing.

![typeof null](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qxnxygf5hije9wnuj6sj.png)
 
On the other hand, undefined is assigned to variables that have just been declared. It represent lack of value. Unlike null, its type if actually same as its name - undefined.

![typeof undefined](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7k4b46aq3spym1k05uxp.png)
 
### Number and BigInt

There are two numeric types in JavaScript: number and BigInt.

There aren't other types of numbers in JavaScript. If you come from other programming languages, you probably used float, decimal, double...

In JavaScript, all of those mentioned above are type of number:

![type of number](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nmk6qyf9gptus3nvmhhg.png)
 
The maximum number of decimals is 17 and integers are accurate up to 15 digits.

BigInt represents integers with arbitrary precision, it is used to store and do operations on large integers (larger than type number supports).

It is created by adding "n" at the end of an integer.

![big int](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tch6gakpzb2ieozd0com.png)

### String

A String is used for textual data, it is a set of values, just like in other programming languages. The difference being they are immutable. Despite that, you can still access each element of the string. Each element has an index, starting from 0 to n-1, n being the length of a string.

![string example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qjx5zwzv4f3q0khpl968.png)
 
### Symbol

A Symbol is a unique and immutable primitive value and may be used as the key of an Object property.

`const person = {name: "Jon"};`

In the example above, "name" is the key of the "person" object and it has a value pair of "Jon".

Thank you for reading this article!