如何在 JavaScript 中檢查值是否為原始值?


在本教程中,我們將瞭解檢查給定資料型別是否為原始型別的方法。

JavaScript 中的資料型別 1. 原始型別 2. 非原始型別

原始資料型別 - 字串、數字、未定義、布林值、空值、符號、大整數。

非原始資料型別 - 物件

原始資料型別/值不是物件,它在語言實現的底層表示。所有原始值都是不可變的,這意味著您不能更改其型別,而是可以將新值重新分配給變數。

要檢查值是否為原始值,我們檢查給定的值是否為物件;如果我們提供的值是物件,則意味著它不是使用某些方法的原始資料型別。

方法 1:使用 Object()

我們將使用嚴格相等運算子檢查給定值是否為物件型別,因為它還會檢查資料型別以及值。它將首先將值轉換為物件,因為我們將透過物件將值作為引數傳遞。如果我們的值是物件,則 object 函式將返回相同的值,並且它將被視為物件,否則嚴格相等運算子將檢查並返回 false,因為型別將不相同。

語法

inputValue !== Object(inputValue);

讓我們定義一個函式來檢查輸入值是否為原始型別。

function isPrimitive(inputValue){
   if(inputValue===Object(inputValue)){
      return "Value is not primitive";
   }
   else{
      return "Value is primitive";
   }
}

示例

在下面的示例中,我們檢查以下值是否為原始值。

  • 空值

  • 數字

  • 字串

  • 字串物件

  • 布林值

  • 陣列

  • 空陣列

  • 物件字面量

<!DOCTYPE html> <html> <head> </head> <body> <h2>Check if the value is primitive or not</h2> <p id="result"></p> </body> <script type="text/javascript"> function isPrimitive(inputValue) { if (inputValue === Object(inputValue)) { console.log("Value is not primitive") document.getElementById("result").innerHTML += inputValue +": not primitive <br>" } else { console.log("Value is primitive") document.getElementById("result").innerHTML += inputValue +": primitive <br>" } } isPrimitive(null) isPrimitive(12) isPrimitive("This is simple string") isPrimitive(new String("This is string object")) isPrimitive(false) isPrimitive([1, 2, 3]) isPrimitive([]) isPrimitive({}) </script> </html>

方法 2:使用 typeof 運算子

在這種方法中,我們將使用typeof運算子檢查資料型別,並且我們知道非原始資料型別始終是物件型別,因此我們將檢查我們的值是否為物件型別。

如果我們的值不是物件或函式型別,則它是原始型別;否則它不是原始型別。我們還必須處理 null 的情況,因為 null 是原始型別值,但typeof如果我們檢查 typeof(null),則會顯示輸出為物件。

function isPrimitive(inputValue){
   if(inputValue==null)
   {
      return "Value is primitive";
   }
   if(typeof(inputValue)=="function" || typeof(inputValue)=="object"){
      return "Value is not primitive"
   }
   else{
      return "Value is not primitive"
   }
}

示例

在下面的示例中,我們檢查不同的值是否為原始值。為了檢查值是否為原始值,我們使用了 typeof 運算子。我們檢查型別是否為函式或物件。如果型別是函式或物件,則該值不是原始型別;否則它是原始型別。

<!DOCTYPE html> <html> <head> </head> <body> <h2>Check if the value is primitive or not</h2> <div id="result"></div> </body> <script type="text/javascript"> function isPrimitive(inputValue){ if(inputValue==null) { return `primitive <br>`; } if(typeof(inputValue)=="function" || typeof(inputValue)=="object"){ return `not primitive <br>`; } else{ return `primitive <br>`; } } let resultDiv = document.getElementById("result"); resultDiv.innerHTML += "12: " + isPrimitive(12); resultDiv.innerHTML += "null: " + isPrimitive(null); resultDiv.innerHTML += "false: " + isPrimitive(false); resultDiv.innerHTML += "[1,2,3]: " + isPrimitive([1,2,3]); resultDiv.innerHTML += `"This is simple string": ` + isPrimitive("This is simple string"); resultDiv.innerHTML += "new String(): " + isPrimitive(new String("This is string object")); resultDiv.innerHTML += "[]: " + isPrimitive([]); resultDiv.innerHTML += "{}: " + isPrimitive({}); resultDiv.innerHTML += "new Date(): " + isPrimitive(new Date()); </script> </html>

因此,我們瞭解了檢查給定值是否為原始型別值或非原始型別值的方法。

更新於: 2022-08-23

3K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告