Open In App

Primitive and Non-primitive data-types in JavaScript

Improve
Improve
Like Article
Like
Save
Share
Report

Every Variable has a data type that tells what kind of data is being stored in a variable. There are two types of data types in JavaScript.

  • Primitive data types 
  • Non-primitive data types

Primitive data types:

The predefined data types provided by JavaScript language are known as primitive data types. Primitive data types are also known as in-built data types.

Below is a list of Primitive Data Types with proper descriptions and examples:

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

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

Undefined

The meaning of undefined is ‘value is not assigned’.

Example: Below is an example.

Javascript




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


Output:

undefined  output

Boolean

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

Example: Below is an example.

Javascript




console.log("value of bool=" + bool);


Output:

boolean output

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);


Output

Value of x=null

BigInt

This 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

Symbol

This 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)

Non-primitive data types:

The data types that are derived from primitive data types of the JavaScript language are known as non-primitive data types. It is also known as derived data types or reference data types.

Below is a list of Non-primitive data types.

Non-primitive Data Types
Object
Array

Below is a list of Non-primitive Data Types with proper descriptions and examples:

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:

  • Using Constructor Function to define an object:
// Create an empty generic object
let obj = new Object();

// Create a user defined object
let mycar = new Car();
  • Using Literal notations to define an object:
// 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);


Output

Luiza  Shaikh

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.


Last Updated : 26 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads