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:
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 output1.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
Records and Tuples - New data structure in JavaScript In this article, we are going to know/learn about a new primitive data structure Record and Tuple. These data structures are not available in official JavaScript because these are just proposals to add in Javascript. Both Records and Tuples are primitive data types and we can only use primitive data
2 min read
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 variab
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