Home > Web Front-end > JS Tutorial > How to check if a value is primitive in JavaScript?

How to check if a value is primitive in JavaScript?

王林
Release: 2023-08-26 16:33:16
forward
881 people have browsed it

JavaScript 中如何检查值是否为原始值?

In this tutorial we will learn about the method to check if a given data type is Original or not.

Data types in JavaScript 1. Primitive data types 2. Non-primitive data types

Primitive data types - string, number, undefined, boolean, null, symbol, bigint.

Non-primitive data type - Object

Primitive data type/value is not an object, it is represented as The bottom layer of language implementation. All primitive values ​​are immutable This means that you cannot change their type, but can reassign the new value of v to variable.

To check if the value is a primitive value, we check if the given value is an object; if The value we provide is an object, which means it is not a primitive data type using some method.

Method 1: Use Object()

We will use strict equality operator to check if the given value is of object type Because it also checks the data type and value. It first converts the value to Object since we will pass the value as parameter via object. If our value is an object, then the object function will return the same object and it will be treated as a object, otherwise the strict equality operator will check and return false because the type does not it's the same.

grammar

inputValue !== Object(inputValue);
Copy after login

Let's define a function to check if the input value is of primitive type.

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

Example

In the example below, we check the following values ​​if they are primitive.

  • null

  • number

  • String

  • String object

  • Boolean value

  • Array

  • Empty array

  • Object literal

<!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>
Copy after login

Method 2: Use typeof operator

In this method we will check the data type using typeof operator and we know that non primitive data types are always object types so we will check if our value is of the following type Object or not.

If our value is not of type object or function, then it is a primitive value; otherwise it is not primitive. We also have to handle the null case, since null is a primitive type value, but typeof will If we check typeof(null) then the output is displayed as object.

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"
   }
}
Copy after login

Example

In the following example, we check whether the different values ​​are original values. Check if Whether the value is a primitive value, we use the typeof operator. We check if the type is function or object. If the type is a function or object, the value is not a primitive type; otherwise it is primitive.

<!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>
Copy after login

Therefore, we need to know the method of checking whether a given value is a primitive type value or a non-primitive value.

The above is the detailed content of How to check if a value is primitive in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template