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);
OutputValue 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);
OutputValue 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:
.png)
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)
Output123422222222222222222222222222222222222n
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);
Outputsymbol
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:
- 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);
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);
Outputvalue 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. |
Similar Reads
Primitive and Reference value in JavaScript
In JavaScript, a variable may store two types of values, Primitive values or Reference values. This article will describe and help to compare both these types of values. Primitive value: JavaScript provides six types of primitive values that include Number, String, Boolean, Undefined, Symbol, and Bi
2 min read
How to pass primitive/object types through functions in JavaScript ?
In this article, we learn how JavaScript primitive/object types passed through functions. First, we will see, what are primitive and object data types in JavaScript: 1. Primitive data type: The predefined data types that are provided by JavaScript are called primitive data type. The primitive data t
4 min read
Variables and Datatypes in JavaScript
Variables and data types are foundational concepts in programming, serving as the building blocks for storing and manipulating information within a program. In JavaScript, getting a good grasp of these concepts is important for writing code that works well and is easy to understand. VariablesA varia
3 min read
Primitive data type vs. Object data type in Java with Examples
Data Types in Java Every variable in java has a data type. Data types specify the size and type of values that can be stored in an identifier. Java language is rich in its data types. The variety of data types available allow the programmer to select the type appropriate to the need of the applicati
6 min read
How to check if the value is primitive or not in JavaScript ?
To check if the value is primitive we have to compare the value data type. As Object is the non-primitive data type in JavaScript we can compare the value type to object and get the required results. Primitive data types are basic building blocks like numbers and characters, while non-primitive data
3 min read
How to get the native type of a value in JavaScript ?
JavaScript variables can store any type of value. The JavaScript typeof operator can be used to find out the native type of value. It returns a string value indicating the type of the value. Syntax: typeof(value) Possible output values of typeof: TypeResultundefined"undefined"null"object"boolean"boo
1 min read
JavaScript Data Structures Practice Problems
The JavaScript Data Structures Practice Problems page covers fundamental data structures such as arrays and strings and advanced structures like stacks and queues. These exercises will help you build a strong foundation for managing data efficiently and solving programming challenges. Data Structure
1 min read
How to return the data type of variable in JavaScript ?
To return the JavaScript data type of a variable we can use the JavaScript typeof operator. In JavaScript, unlike many other programming languages, we do not specify the type of a variable while declaring it, rather the variable's type is automatically inferred based on the value it holds. In other
3 min read
Types of Arrays in JavaScript
A JavaScript array is a collection of multiple values at different memory blocks but with the same name. The values stored in an array can be accessed by specifying the indexes inside the square brackets starting from 0 and going to the array length - 1([0]...[n-1]). A JavaScript array can be classi
3 min read
How to read properties of an Object in JavaScript ?
Objects in JavaScript, it is the most important data type and forms the building blocks for modern JavaScript. These objects are quite different from JavaScriptâs primitive data-types(Number, String, Boolean, null, undefined, and symbol) in the sense that these primitive data-types all store a singl
2 min read