JavaScript Data Types

What are Data Types?

Data types define the type of data a variable can store. JavaScript is a dynamically typed language.

1. Primitive Data Types

Primitive data types are the most basic data types in JavaScript. They are immutable (cannot be changed) and hold a single value.

let name = "JavaScript"; // String
let age = 25;            // Number
let isActive = true;     // Boolean
let x;                   // Undefined
let y = null;            // Null
let bigVal = 123456789n; // BigInt
        
Output will appear here...

2. Non-Primitive Data Types

Non-primitive data types (Reference types) are objects. They can hold collections of values and more complex entities.

// Object
let student = {
    name: "Ravi",
    age: 20
};

// Array
let colors = ["Red", "Green", "Blue"];

// Function
function greet() {
    return "Hello";
}
        
Output will appear here...

Type of Operator

The typeof operator returns a string indicating the data type of a variable or expression.

let num = 10;
document.write(typeof num);    // "number"
document.write(typeof "Hi");   // "string"
document.write(typeof true);   // "boolean"
document.write(typeof null);   // "object" (Known JS bug)
        
Output will appear here...

Comparison Table

Feature Primitive Non-Primitive
Stored By Value By Reference
Values Single Multiple
Example String, Number Array, Object
✅ JavaScript automatically assigns data types.

Practice Questions

Test your understanding of JavaScript Data Types.

Easy

Q1: Declare variables with different primitive data types and print their types.

  • A string for a name
  • A number for an age
  • A boolean for student status
Easy

Q2: Identify the data type of an unassigned variable.

  • Declare a variable without assigning any value to it.
  • Print the variable itself.
  • Print the type of the variable using typeof.
Medium

Q3: Demonstrate the difference between null and undefined.

  • Create a variable naturally assigned to undefined.
  • Create another variable explicitly assigned to null.
  • Print the typeof for both.
Medium

Q4: Create an object and check the data types of its properties.

  • Create a user object with properties: name (string) and age (number).
  • Check and print the data type of the object itself.
  • Check and print the data type of user.name and user.age.
Hard

Q5: Write a program to verify if a variable is an array.

  • Declare an array of numbers: [10, 20, 30].
  • Print the result of typeof on this array to see what it returns.
  • Write the correct JavaScript method to definitively verify it is an array.

Answer

Code: