JavaScript中的值类型包含哪些数据类型?并确认number、boolean、string、null、undefined、bigint、symbol是否为值类型
Great question! Let's break this down clearly for you. First off, you're right that objects, functions, and arrays fall into JavaScript's reference types category. Now let's dive into the primitive (value) types you're asking about.
What counts as a value type in JavaScript?
In ECMAScript standards, primitive types (the official term for what you're calling "value types") are the most basic data types that store actual values directly, rather than pointing to a memory reference like objects do.
There are exactly 7 primitive types defined in modern JavaScript, and they're exactly the ones you listed:
number: Covers integers, floating-point numbers, plus special values likeNaNandInfinityboolean: Only has two possible values:trueandfalsestring: Even though strings have object-like methods (like.toUpperCase()), they're still primitive values at their corenull: A special primitive representing an intentional empty value (fun fact:typeof nullreturns"object"—that's a long-standing JS bug, but it's still officially a primitive type)undefined: Represents an uninitialized value; variables declared without assignment default to thisbigint: Used for integers larger than the maximum safe value ofnumber(written with annsuffix, e.g.,9007199254740991n)symbol: Unique, immutable values often used as unique keys for object properties
Confirming your list
To directly answer your question: all the types you mentioned—number, boolean, string, null, undefined, bigint, symbol—are absolutely value types (primitive types).
A quick way to tell them apart from reference types: when you assign a primitive value to another variable or pass it as a function argument, you're copying the actual value. With reference types, you're copying a pointer to the same object in memory. Here's a quick example to illustrate:
// Value type behavior let num1 = 25; let num2 = num1; num2 = 50; console.log(num1); // Logs 25—num2 was a copy, so changing it doesn't affect num1 // Reference type behavior let user1 = { name: "Steven" }; let user2 = user1; user2.name = "Steve"; console.log(user1.name); // Logs "Steve"—both variables point to the same object
内容的提问来源于stack exchange,提问作者Steven

