💻

JavaScript Primitives and Objects

Jul 5, 2024

JavaScript Primitives and Objects

Introduction

  • Difference between primitive data types and objects in JavaScript.

Primitive Data Types

  • Fundamental, built-in data types in JavaScript.
  • 7 Primitive Data Types: NN BB SS U
    • NN : Null, Number
    • BB : Boolean, BigInt
    • SS : String, Symbol
    • U: Undefined
  • Purpose of Data Types: The type of content that can be stored in a variable, e.g., strings, numbers.

Examples of Primitive Data Types:

  • Declaration examples: let a = null; let b = 42; // number let c = true; // boolean let d = BigInt(567); // BigInt let e = "Harry"; // string let f = Symbol("I am a nice symbol"); // symbol let g = undefined; // undefined
  • Console Logging: console.log(a, b, c, d, e, f, g);
  • Type Checking: console.log(typeof d); // BigInt
  • null vs undefined:
    • null: Explicitly signifies no value.
    • undefined: Default state of a variable that has not been assigned a value.

Non-Primitive Data Types

  • Objects: Key-value pairs.
  • Key: Simple strings or numbers.
  • Value: Any data type.

Object Example

  • Creation and Usage: const item = { "Harry": true, "Shubh": false, "Lovish": 67, "Rohan": undefined }; console.log(item["Harry"]); // true console.log(item["Rohan"]); // undefined console.log(item["Shubh"]); // false
  • Use Cases:
    • Mapping student marks, item prices, student-classroom assignments.
    • Easy value lookup using keys.

Conclusion

  • Two Types of Data Types in JavaScript:
    • Primitive: NN BB SS U
    • Non-Primitive: Object
  • Importance of understanding data types for JavaScript mastery.

Next Steps: More detailed exploration of symbols and BigInt in future videos.