Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Primitive and Non-primitive data-types in JavaScript

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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.

  • Primitive data types 
  • 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.

Below is a list of Primitive Data Types with proper descriptions and examples:

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

6. BigInt: This 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

Javascript




var bigNum = 123422222222222222222222222222222222222n
console.log(bigNum)

Output:

123422222222222222222222222222222222222n

7. Symbol: This data type is used to create objects which will always be unique. these objects can be created using Symbol constructor.

Javascript




var sym = Symbol("Hello")
console.log(typeof(sym));
console.log(sym);

Output:

Symbol Output

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.

Non-primitive Data Types
Object
Array

Below is a list of Non-primitive Data Types with proper descriptions and examples:

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
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

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:

PrimitiveNon-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.

My Personal Notes arrow_drop_up
Last Updated : 10 Mar, 2023
Like Article
Save Article
Similar Reads
Related Tutorials