Explore the fundamental data types in JavaScript, including both primitive and reference types, which form the basis for handling and manipulating data in programs.
This code snippet demonstrates how various data types are used in JavaScript:
// Primitive types
let name = "Alice"; // String
let age = 25; // Number
let isStudent = true; // Boolean
let unknown = undefined; // Undefined
let empty = null; // Null
let id = Symbol("id"); // Symbol
let bigIntValue = 123456789012345678901234567890n; // BigInt
// Reference types
let person = { name: "Alice", age: 25 }; // Object
let colors = ["red", "green", "blue"]; // Array
function greet() { console.log("Hello"); } // Function
This code demonstrates:
string
, number
, boolean
, null
, undefined
, symbol
, and bigint
. They are immutable and stored by value.objects
, arrays
, and functions
. These are mutable and stored by reference.typeof
for most types and Array.isArray()
or instanceof
for reference types.
Q1: What type does typeof null
return?
Answer: B. "object"
Q2: Which of the following is a reference type?
Answer: C. Object
Q3: What is the result of typeof NaN
?
Answer: C. "number"
Q4: Which statement is true about BigInt?
Answer: B. It's used for representing very large integers.