Primitive and Non-primitive data-types in JavaScript
Last Updated :
13 May, 2024
In JavaScript, variables hold values, and each value possesses a data type that indicates the nature of the stored information. Broadly, JavaScript classifies data types into two categories: Primitive data types and Non-primitive data types. These distinctions are essential for understanding how data is handled and manipulated within the language. Let us discuss it one by one.
1. 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);
OutputValue 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);
OutputValue of str=Hello All
Value of str1=Welcome to my new house
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:
undefined output
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
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);
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
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)
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.
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);
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. |
Share your thoughts in the comments
Please Login to comment...