primitive(primitive value、primitive data type)-基本型別(或譯為原始型別)指的是非物件的型別,並且不具有方法。在 JavaScript 裡,共有六種基本型別:stringnumberbooleannullundefinedsymbol(於 ECMAScript 2015 新推出)。




This example would help you understand the fact that primitive values are immutable.


// Using a string method doesn't mutate the string
var bar = "baz";
console.log(bar);               // baz
console.log(bar);               // baz

// Using an array method mutates the array
var foo = [];
console.log(foo);               // []
console.log(foo);               // ["plugh"]

// Assignment gives the primitive a new (not a mutated) value
bar = bar.toUpperCase();       // BAZ

A primitive can be replaced, but it can't be directly altered.

Another Example [ Step-by-step ]

The following example will help you going through how JavaScript deal with Primitives.


// The Primitive 
let foo = 5;

// Defining a function that should change the Primitive value
function addTwo(num) {
   num += 2;
// Another function trying to do the same thing
function addTwo_v2(foo) {
   foo += 2;

// Calling our first function while passing our Primitive as an argument
// Getting the current Primitive value
console.log(foo);   // 5

// Trying again with our second function...
console.log(foo);   // 5

Did you expect it to be 7 instead of 5? If so, read how this code runs:

  • For both the addTwo and addTwo_v2 functions calls, JavaScript looks up the value for the identifier foo. It correctly finds our variable instanciated with our first statement
  • After finding it, JavaScript passes that argument to the functions as a parameter
  • Before executing the statements inside the functions' bodies, JavaScript takes a copy of the original passed argument (which is a primitive) and creates a local copy. These copies, existing only inside the functions' scopes, are accessible via the identifiers we specified in the functions' definitions (num for addTwofoo for addTwo_v2)
  • Then, the functions' statements are exectued:
    • In the first function, a local num argument had been created. We are increasing its value by 2, not the original foo's value!
    • In the second function, a local foo argument had been created. We are increasing its value by 2, not the original (external) foo's value! Also, in this situation, the external foo variable cannot be accessed in any way. This is because of JavaScript's lexical scoping and the resulting variable shadowing. The local foo hides the external foo. For more information, see Closures.
  • In conclusion, any changes inside our functions won't affect the ORIGINAL foo at all, as we are working on our copies of it

That's why Primitives are Immutable. Because, we aren't working on them directly, we took our copy and keep working with it, without touching the original one.

JavaScript 中基本型別的包裹物件

除了 null 以及 undefined,所有的基本型別都有個和其等值的包裹物件:

  • String 對應 string 基本型別。
  • Number 對應 number 基本型別。
  • Boolean 對應 Boolean 基本型別。
  • Symbol 對應 Symbol 基本型別。

而包裹物件的 valueOf() 方法將返回基本型別的值。


General knowledge


此頁面的貢獻者: qwer5656, jackblackevo, Snailpool
最近更新: qwer5656,