Primitive and Nonprimitive datatypes in JavaScript (original) (raw)

Primitive and Non-primitive data-types in JavaScript

Last Updated : 21 Aug, 2024

Variables hold values, and every value has a specific data type that defines the kind of information it holds. These data types are broadly categorized into two groups: **Primitive Data Types and **Non-Primitive Data Types. Let us discuss it one by one.

**1. Primitive Data Types

Primitive data types are the built-in data types provided by JavaScript. They represent single values and are not mutable. JavaScript supports the following primitive data types:

**1.1 Number:

Number data type in JavaScript can be used to hold decimal values as well as values without decimals.

**Example: Below is an example.

JavaScript `

let x = 250;
let y = 40.5;
console.log("Value of x=" + x);
console.log("Value of y=" + y);

`

Output

Value of x=250 Value of y=40.5

**1.2 String:

The string data type in JavaScript represents a sequence of characters that are surrounded by single or double quotes.

**Example: Below is an example.

JavaScript `

let str = 'Hello All'; let str1 = "Welcome to my new house"; console.log("Value of str=" + str); console.log("Value of str1=" + str1);

`

Output

Value of str=Hello All Value of str1=Welcome to my new house

**1.3 Undefined:

This means that a variable has been declared but has not been assigned a value, or it has been explicitly set to the value `undefined`.

**Example: Below is an example.

JavaScript `

let x; console.log(x); // Outputs: undefined

`

**Output:

Screenshot-(6)

undefined output

**1.4 Boolean:

The boolean data type can accept only two values i.e. true and false.

**Example: Below is an example.

JavaScript `

let x; console.log(x); // Outputs: undefined

`

**Output:

boolean output

**1.5 Null:

This data type can hold only one possible value that is null.

**Example: Below is an example.

JavaScript `

let x = null; console.log("Value of x=" + x);

`

**1.6 BigInt:

BigInt data type can represent numbers greater than 253-1 which helps to perform operations on large numbers. The number is specified by writing 'n' at the end of the value

**Example: Below is an example.

JavaScript `

let bigNum = 123422222222222222222222222222222222222n console.log(bigNum)

`

Output

123422222222222222222222222222222222222n

**1.7 Symbol:

Symbol data type is used to create objects which will always be unique. these objects can be created using Symbol constructor.

**Example: Below is an example.

JavaScript `

let sym = Symbol("Hello") console.log(typeof(sym)); console.log(sym);

`

Output

symbol Symbol(Hello)

**2. **Non-primitive Data Types

Non-primitive data types, also known as reference types, are objects and derived data types. They can store collections of values or more complex entities. The two key non-primitive data types in JavaScript are:

Below is a list of Non-primitive data types.

**2.1 Object:

**An **object in Javascript is an entity having properties and methods. Everything is an object in javascript.

How to create an object in javascript:

// Create an empty generic object
let obj = new Object();

// Create a user defined object
let mycar = new Car();

// An empty object
let square = {};

// Here a and b are keys and
// 20 and 30 are values
let circle = {a: 20, b: 30};

**Example: Below is an example.

JavaScript `

// Creating object with the name person let person = { firstName: "Luiza", lastName: "Shaikh", };

// Print the value of object on console
console.log(person.firstName 
    + "  " + person.lastName);

`

**2.2 Array:

With the help of an array, we can store more than one element under a single name.

**Ways to declare a **single-dimensional array:

// Call it with no arguments
let a = new Array();

// Call it with single numeric argument
let b = new Array(10);

// Explicitly specify two or
// more array elements
let d = new Array(1, 2, 3, "Hello");

**Example: Below is an example.

JavaScript `

let a = new Array();
let b = new Array(10);
let d = new Array(1, 2, 3, "Hello");
console.log("value of a=" + a);
console.log("value of b" + b);
console.log("value of d=" + d);

`

Output

value of a= value of b,,,,,,,,, value of d=1,2,3,Hello

**Note: JavaScript does not support two-dimensional arrays. but we can do this by creating an array of an array.

**Difference Between Primitive vs Non-Primitive

**Primitive **Non-Primitive
Primitive Data types are predefined. Non-Primitive data types are created by the programmer
Primitive Data types will have certain values. Non-Primitive data types can be NULL.
Size depends on the type of data structure. Size is not fixed
Examples are numbers and strings. Examples are Array and Linked List.
It can start with a lowercase. It can start with uppercase.