Technical Sharing December 2, 2025 约 26 分钟阅读

Understanding JavaScript Basic Data Types

JavaScript is a loosely typed and dynamic language. This means you don't have to declare the type of a variable ahead of time; the type will be determined automatically while the program is being processed. JavaScript data types are divided into two main categories: Primitive Types and Reference Types.

Primitive Types

JavaScript currently has 7 primitive data types. Primitive types are accessed by value, meaning you manipulate the actual value stored in the variable.

1. String

Used to represent textual data.

let name = "Alice";
let message = 'Hello, World!';
let template = `Hello, ${name}`; // Template literal

2. Number

JavaScript has only one number type, used to represent both integers and floating-point numbers.

let integer = 123;
let float = 3.14;
let infinite = Infinity;
let notANumber = NaN; // Not a Number

3. Boolean

Has only two values: true and false. Commonly used in conditional statements.

let isDone = false;
if (isDone) {
    console.log("Completed");
}

4. Undefined

When a variable is declared but not assigned a value, its value is undefined.

let x;
console.log(x); // undefined

5. Null

null represents an "empty" value, usually used to indicate that a variable is intended to point to an object but is currently empty.

let obj = null;

Note: typeof null returns "object". This is a historical bug in JavaScript, but null is indeed a primitive type.

6. Symbol

Introduced in ES6, represents a unique value. Often used to define unique property keys for objects.

let sym1 = Symbol("id");
let sym2 = Symbol("id");
console.log(sym1 === sym2); // false

7. BigInt

Introduced in ES2020, used to represent integers of arbitrary precision. The standard Number type can lose precision beyond the safe integer range ($2^{53} - 1$).

const huge = 1234567890123456789012345678901234567890n;

Reference Types (Objects)

Reference type values are objects stored in memory. JavaScript does not allow direct access to memory locations, meaning you cannot directly manipulate the object's memory space. When you manipulate an object, you are actually manipulating a reference to the object, not the actual object itself.

Object

An object is a collection of data and functionality.

  • Plain Object: { key: 'value' }
  • Array: [1, 2, 3]
  • Function: function() {}
  • Date: new Date()
  • RegExp: /abc/
let person = {
    name: "Bob",
    age: 30
};

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

function greet() {
    console.log("Hi!");
}

Primitives vs. Reference Types

FeaturePrimitive TypesReference Types
StorageStack MemoryHeap Memory
AccessBy ValueBy Reference
CopyingCopies actual value, independentCopies reference address, points to same object
ComparisonCompares valuesCompares reference addresses

Example: Copying Values

Primitive Type:

let a = 10;
let b = a;
b = 20;
console.log(a); // 10 (a is not affected by b)

Reference Type:

let obj1 = { name: "Tom" };
let obj2 = obj1; // Copies the reference address
obj2.name = "Jerry";
console.log(obj1.name); // "Jerry" (obj1 is affected)

Type Checking

typeof

Used to check primitive data types (except null).

typeof "str";   // "string"
typeof 123;     // "number"
typeof true;    // "boolean"
typeof undefined; // "undefined"
typeof Symbol(); // "symbol"
typeof 1n;      // "bigint"
typeof {};      // "object"
typeof [];      // "object"
typeof null;    // "object" (special case)
typeof function(){}; // "function" (special case, but also an object)

instanceof

Used to check reference types, determining if the prototype property of a constructor appears anywhere in the object's prototype chain.

[] instanceof Array; // true
{} instanceof Object; // true

Summary

Understanding JavaScript's data types and how they are stored (Stack vs. Heap) is fundamental to writing robust code. Especially when passing and modifying objects, always be aware of the side effects that reference types can introduce.