Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Primitive and Non-primitive data-types in JavaScript

  • Difficulty Level : Easy
  • Last Updated : 26 Nov, 2021

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

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.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!