Introduction  to
Primitive types in JavaScript with examples
Tags: javascript, codenewbie, beginners, webdev
Published: 2020-11-17 07:43:14

In this article, we will try to understand the primitive types in javascript.
This article is for beginners only.

A primitive value or data type is something that is not an object and has no methods associated with it. In other words, primitive data is simple data with no additional methods and properties. 

There are 7 primitive data types in JavaScript.



  [1. boolean](#c1)
  [2. null](#c2)
  [3. undefined](#c3)
  [4. number](#c4)
  [5. string](#c5)
  [6. symbol](#c6)
  [7. bigint](#c7)


We can use  **typeof (value)** operator to know the data type of any given value.

#### Example

```javascript
 
typeof 1;         // number
typeof "dev.to;"  // string
typeof null;      // object
typeof undefined; // undefined
typeof true;      // boolean
typeof 1n;        // bigint

let sym = Symbol("key");
typeof key;       // symbol


```

## 1.boolean  <a name="c1"></a>

A **boolean** is a data type that can only take two values, which are, **true** and **false**.

A boolean value is commonly used in comparison and conditional operations.

We can use **Boolean()** constructor to create a boolean object. However, this is generally considered a bad practice and should be avoided. 

```javascript

let x = new Boolean(true);

typeof  x;  // boolean

```

Don't confuse a boolean primitive type with a Boolean object.

 

## 2.null  <a name="c2"></a>

The value **null** represents the absence of the object.

#### Example

```javascript

let boo = null;
typeof boo; //null

```

In the above example, the variable *boo* exists but it doesn't have any value associated with it. 


## 3. undefined  <a name="c3"></a>

**undefined** is a value that is automatically assigned to variables that have been declared 

#### Example

```javascript
var x;
typeof x;  // undefined

```

## 4. number  <a name="c4"></a>

A **number** data type contains numerical values. You can define positive, decimal (floating point), and negative values. It also has a positive and negative Infinite value.

We can use **Number()** constructor to initialize an object of number datatype.

#### Example

```javascript
var num = 123;
typeof num;   // number

// using constructor

var boo = new Number(123)
typeof boo;   // object
typeof boo.valueOf();  // number


```



## 5. string  <a name="c5"></a>

A **string** is a data type that consists of a character or sequence of characters in single quotes('example') or double quotes("example") or in back-tick quotes.

We can use the **String()** constructor function to build an object of type string.

#### Example

```javascript
let string1 = "Double quotes";
let string2 = 'Single quotes';


typeof string1;  // string 

let string4 = new String("string constuctor");
typeof string4;  // object
typeof string4.valueOf();  // stirng

```
## 6. symbol  <a name="c6"></a>

A **symbol** is a datatype that provides an anonymous, unique value that can be used as an object property.

Symbols are introduced in ES6.

A symbol doesn't have a constructor so we cannot create a symbol object using the *new* keyword.

#### Example

```javascript

let sym1  = new Symbol('a'); // TypeError

let sym2 = Symbol('a');  // symbol is created
let sym3 = Symbol('a');  // symbol is created

sym2  === sym3;  // false

```

The last sentence is false because both sym2 and sym3 are unique keys.


refer this [page](https://developer.mozilla.org/en-US/docs/Glossary/Symbol) for more details about symbols.


## 7. bigint  <a name="c7"></a>

**bigint** primitive datatype is introduced in ES10.

Before ES10, the maximum value of a number in JavaScript is 9007199254740991 or Number.MAX_SAFE_INTEGER. In order to overcome this limitation **bigint** were introduced.

The maximum value of **bigint** is 2^51 -1.

A bigint contains *n* as a suffix to a number.

#### Example


```javascript
let x = 1;
let y = 1n;

typeof x;  // number
typeof y;  // bigint

```



Thankyou for reading the article please provide your valuable feedback in the comments.