DEV Community

Cover image for Introduction  to
Primitive types in JavaScript with examples
vineethsagar
vineethsagar

Posted on • Updated on

Introduction to Primitive types in JavaScript with examples

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
2. null
3. undefined
4. number
5. string
6. symbol
7. bigint

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

Example


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


Enter fullscreen mode Exit fullscreen mode

1.boolean

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.


let x = new Boolean(true);

typeof  x;  // boolean

Enter fullscreen mode Exit fullscreen mode

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

2.null

The value null represents the absence of the object.

Example


let boo = null;
typeof boo; //null

Enter fullscreen mode Exit fullscreen mode

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

3. undefined

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

Example

var x;
typeof x;  // undefined

Enter fullscreen mode Exit fullscreen mode

4. number

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

var num = 123;
typeof num;   // number

// using constructor

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


Enter fullscreen mode Exit fullscreen mode

5. string

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

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


typeof string1;  // string 

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

Enter fullscreen mode Exit fullscreen mode

6. symbol

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


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

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

sym2  === sym3;  // false

Enter fullscreen mode Exit fullscreen mode

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

refer this page for more details about symbols.

7. bigint

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

let x = 1;
let y = 1n;

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

Enter fullscreen mode Exit fullscreen mode

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

Top comments (2)

Collapse
 
alfredosalzillo profile image
Alfredo Salzillo

please add the javascript tag to the code blocks

Collapse
 
vineethsagar profile image
vineethsagar

Added. Thanks for suggestion.