Back to the 2019 paper

Module III: Basics of Web Programming

20197m

Explain various data types used in JavaScript with the help of examples.

Worked SolutionAI Assisted

Answer: Data Types in JavaScript with Examples

1. Introduction

JavaScript is a dynamically typed (loosely typed) language, meaning variables do not require explicit type declarations; data types are automatically determined at runtime based on the assigned value.

Data types in JavaScript are divided into two primary categories:

  1. Primitive Data Types (Immutable, passed by value)
  2. Non-Primitive / Reference Data Types (Mutable, passed by reference)
                               +-----------------------------+
                               |    JAVASCRIPT DATA TYPES    |
                               +--------------+--------------+
                                              |
                     +------------------------+------------------------+
                     |                                                 |
                     v                                                 v
           +--------------------+                            +--------------------+
           |  PRIMITIVE TYPES   |                            |  REFERENCE TYPES   |
           | Number, String,    |                            | Object, Array,     |
           | Boolean, Undefined,|                            | Function, Date,    |
           | Null, BigInt,      |                            | Map, Set           |
           | Symbol             |                            |                    |
           +--------------------+                            +--------------------+

2. Primitive Data Types

1. Number

  • Represents both integer and floating-point numeric values (IEEE 754 64-bit float). Also includes special values: Infinity, -Infinity, and NaN (Not-a-Number).
let age = 21;
let price = 99.95;
let invalid = "text" / 2; // Returns NaN

2. String

  • Represents textual data enclosed within single quotes ('...'), double quotes ("..."), or backticks for template literals (`...`).
let name = "Rohan";
let message = `Hello, ${name}!`; // Template literal interpolation

3. Boolean

  • Represents a logical entity with only two possible values: true or false.
let isLoggedIn = true;
let hasPaid = false;

4. Undefined

  • A variable that has been declared but not yet assigned a value automatically holds the value undefined.
let x;
console.log(x); // Output: undefined

5. Null

  • Represents the intentional absence of any object value (represents "empty" or "nothing").
let currentUser = null; // Explicitly no user logged in

6. BigInt

  • Used for arbitrarily large integers that exceed the safe integer limit of Number (2^53 - 1). Created by appending n to the integer.
let largeNumber = 9007199254740991123456789n;

7. Symbol

  • Represents a unique, immutable identifier, commonly used to create private or collision-resistant object keys.
let id1 = Symbol("id");
let id2 = Symbol("id");
console.log(id1 === id2); // false (each Symbol is globally unique)

3. Non-Primitive (Reference) Data Types

1. Object

  • A collection of key-value pairs used to model complex entities.
let student = {
    name: "Aman",
    rollNo: 105,
    branch: "CSE",
    greet: function() { console.log("Hello from " + this.name); }
};

2. Array

  • An ordered list/collection of elements (can hold mixed data types).
let subjects = ["Web Tech", "Networks", "Compiler Design", 2026, true];
console.log(subjects[0]); // Output: "Web Tech"

3. Function

  • Functions in JavaScript are First-Class Objects, meaning they can be assigned to variables, passed as arguments, and returned from other functions.
function add(a, b) {
    return a + b;
}

4. Checking Data Types using the typeof Operator

console.log(typeof 42);             // "number"
console.log(typeof "Hello");        // "string"
console.log(typeof true);           // "boolean"
console.log(typeof undefined);      // "undefined"
console.log(typeof 100n);           // "bigint"
console.log(typeof Symbol("key"));  // "symbol"
console.log(typeof { a: 1 });       // "object"
console.log(typeof [1, 2, 3]);      // "object"
console.log(typeof function() {});  // "function"

// Historical JavaScript quirk:
console.log(typeof null);           // "object" (known legacy bug in JS)

5. Dynamic Typing Example

let dynamicVar = 100;         // Currently a Number
dynamicVar = "Now a String";  // Now a String
dynamicVar = [1, 2, 3];       // Now an Array (Object)

Similar questions