Primitive and Non-primitive data-types in JavaScript.
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.
- 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:
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:
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:
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:
let x;
console.log(x); // Outputs: undefined
Output:
Click to enlarge
boolean output
1.5 Null:
This data type can hold only one possible value that is null.
Example:
let x = null;
console.log("Value of x=" + x);
Output
Value of x=null
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.
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:
let sym = Symbol("Hello")
console.log(typeof(sym));
console.log(sym);
Output
symbol
Symbol(Hello)
- 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:
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:
// Creating object with the name person
let person = {
firstName: "MS",
lastName: "Praveen",
};
// Print the value of object on console
console.log(person.firstName
+ " " + person.lastName);
Output
MS Praveen
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:
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.
Primitive data types:
Primitive Data types are predefined.
Primitive Data types will have certain values.
Size depends on the type of data structure.
Examples are numbers and strings.
It can start with a lowercase.
Non-primitive data types:
Non-Primitive data types are created by the programmer
Primitive Data types will have certain values.
Non-Primitive data types can be NULL.
Size is not fixed.
Examples are Array and Linked List.
It can start with uppercase.
Reference website geeksforgeeks.org
Top comments (0)