Primitive and Non-primitive data-types in JavaScript

13 Jun, 2022
Data Types: 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 namely Primitive data types and 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.
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.
There are five types of primitive data types in Javascript.
1. Number: Number data type in javascript can be used to hold decimal values as well as values without decimals.
Javascript
<script>    let x = 250;    let y = 40.5;    console.log("Value of x=" + x);    console.log("Value of y=" + y);</script>
Output:
number output
2. String: The string data type in javascript represents a sequence of characters that are surrounded by single or double quotes.
Javascript
<script>    let str = 'Hello All';    let str1 = "Welcome to my new house";    console.log("Value of str=" + str);    console.log("Value of str1=" + str1);</script>
Output:
string output
3. Undefined: The meaning of undefined is ‘value is not assigned’.
Javascript
<script>    console.log("Value of x=" + x);</script>
Output:
undefined  output
4. Boolean: The boolean data type can accept only two values i.e. true and false.
Javascript
<script>    console.log("value of bool=" + bool);</script>
Output:
boolean output
5. Null: This data type can hold only one possible value that is null.
Javascript
<script>    let x = null;    console.log("Value of x=" + x);</script>
null  output
The non-primitive data types are as follows:
1. Object: 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
var obj = new Object();

// Create a user defined object
var mycar = new Car();
Using Literal notations to define an object:
// An empty object
var square = {};

// Here a and b are keys and
// 20 and 30 are values
var circle = {a: 20, b: 30};
Example:
Javascript
<script>     // 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);</script>
object output
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
var a = new Array();

// Call it with single numeric argument
var b = new Array(10);

// Explicitly specify two or
// more array elements
var d = new Array(1, 2, 3, "Hello");
Example:
Javascript
<script>    var a = new Array();    var b = new Array(10);    var 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);</script>
Output:
array output
JavaScript does not support two-dimensional arrays. but we can do this by creating an array of an array.
Primitive vs Non-Primitive 
Primitive
Non-Primitive
Primitive Data types are predefined 
Non-Primitive data types are created by programmer
Primitive Data types will have certian values 
Non-Primitive data types can be NULL
Size depends on type on data structure
Size are not fixed
Examples are numbers, string
Example are Array, Linked List
It can start with lowercase
It can start with uppercase
guptavivek0503
Data Types
JavaScript-Questions
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Difference Between PUT and PATCH Request
How to Open URL in New Tab using JavaScript ?
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?