What is Primitive data types in JavaScript?

After we have studied the data types in JavaScript, this time, you’ll understand what is primitive and non-primitive data types in JavaScript.

In this article, you’ll discover the fundamental concepts of primitive and non-primitive data types in JavaScript.

Keep reading to learn about the six primitive data types and the two non-primitive data types, along with their key differences and usage examples.

What is primitive data types in JavaScript?

Primitive data types in JavaScript are the most basic data types that represent a single value. They are not objects and do not have methods.

There are six (6) primitive data types in JavaScript: Number, String, Boolean, BigInt, Undefined, Null, and Symbol.

Each of these data types represents a different kind of value and has its own set of operations that can be performed on it.

For example, you can perform arithmetic operations on numbers, concatenate strings, and use boolean values in conditional statements.

Moreover, primitives in JavaScript are unchangeable; this means you can’t modify them. It’s crucial to distinguish between a primitive value and a variable that holds it.

While you can assign a new value to the variable, you can’t directly change the original value like you can with objects, arrays, or functions.

JavaScript doesn’t provide tools to modify primitive values.

What are the seven (7) primitive data types in JavaScript

Here’s a concise overview with examples of the seven primitive data types in JS:

1. String

A string is a sequence of characters used to represent text. It is created by enclosing characters in single or double quotes.

Single quotes: ‘Itsourcecode’

Double quotes: “Itsourcecode”

Backticks: `Itsourcecode`

For example:

let samplestring = "Hi, Welcome to Itsourcecode!";
console.log(samplestring);

Output:

Hi, Welcome to Itsourcecode!

You also have the ability to include quotation marks within a string, provided they do not match the quotation marks that enclose the string.

let samplestring = 'Welcome to "Itsourcecode"';

2. Number

A number is a numerical value, which can be an integer or a floating-point number.

The majority of programming languages encompass a variety of numeric data types:

✔ Whole numbers (integers):

  1. byte (8-bit)
  2. short (16-bit)
  3. int (32-bit)
  4. int (32-bit)

✔ Real numbers (floating-point):

  1. float (32-bit)
  2. double (64-bit)

In JavaScript, numbers are only of a single type: double (64-bit floating point).

For example:

let samplenumber = 3.14; (With decimal)

let samplenumber = 3; (Without decimal)

The number data type can also represent +Infinity, -Infinity, and NaN (not a number).

Example 1:

const samplenumbe1 = 10/0;
console.log(samplenumbe1);

Output:

 Infinity

Example 2:


const samplenumbe2 = -10/0;
console.log(samplenumbe2);

Output:

-Infinity

Example 3:

const samplenumbe3 = "xyz"/1; 
console.log(samplenumbe3); 

Output:

NaN

📌 Note: Strings can’t be divided into any numbers that’s the reason why it will return Nan.

3. BigInt

A BigInt is an integer with arbitrary precision, meaning it can represent numbers larger than the maximum safe integer in JavaScript (2^53 – 1).

It is created by appending n to the end of an integer literal.

For example:

 let sampleBigInt = 1234567890123456789012345678901234567890n;

📌Please be aware that BigInt was introduced in a more recent JavaScript version and is not compatible with several browsers, including Safari.

4. Boolean

A boolean represents a logical value, either true or false.

For example:

let a = 10;
let b = 10;
let c = 20;
console.log(a == b);    // true   
console.log(b == c);    // false 

Output:

true
false

5. Undefined

A variable that has been declared but has not been assigned a value is undefined.

For example:

let sampleVariable;
console.log(sampleVariable);

Output:

undefined

6. Null

The value represents the intentional absence of any object value. It is often used to indicate that a variable should have no value.

In a simple understanding, null is a special value that means something is empty or unknown in JavaScript.

For example:

let sampleVariable = null;

7. Symbol

A symbol is a unique and immutable primitive value that can be used as the key of an object property. It is created using the Symbol() function.

For example:

let value = Symbol('Itsourcecode');

What is non-primitive data types in JavaScript?

Non-primitive data types in JavaScript are derived from primitive data types and are also known as reference data types. They include Object and Array.

Unlike primitive data types, non-primitive data types can hold multiple values and can be used to store and manage collections of data.

Non-primitive data types hold a reference to the actual data, not the data itself.

What are the non-primitive data types in JavaScript?

The following are the two (2) non-primitive data types in JS.

Object

Here is an example of using an Object in JavaScript:

let user = {
firstName: "Itsourcecode",
lastName: "Smith",
};
console.log(user.firstName);

Output:

Itsourcecode

Array

Here is an example of using an Array in JavaScript:

let subjects = ["Math", "English", "Programming"];
console.log(subjects[2]);subjects

Output:

Programming

Difference between primitive and non-primitive data types

Here is a table that compares the differences between primitive and non-primitive data types in JS:

PropertyPrimitive Data TypesNon-Primitive Data Types
DefinitionThe most basic data types that represent a single value. They are not objects and do not have methods.Derived from primitive data types and are also known as reference data types. They include Object and Array.
Values
Can hold only one value of a specific type.Can hold multiple values of different types.
Storage
Store the data directly.Hold a reference to the actual data, not the data itself.
Number, String, Boolean, BigInt, Undefined, Null, and Symbol.Object and Array

Conclusion

This article discusses the primitive and non-primitive data types in JavaScript.

We have explored the six primitive data types, including Number, String, Boolean, BigInt, Undefined, Null, and Symbol, each with its unique characteristics and use cases.

It’s essential to understand that primitive data types represent single values and are immutable.

Additionally, we have introduced the two non-primitive data types, Object, and Array, which are reference data types derived from primitive types.

We are hoping that this article provides you with enough information. That will help you understand the primitive data types in JavaScript.

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Leave a Comment