Type judgment

Data types should be the first basic js lesson. In everyday development, there are many types of judgment and conversion scenarios.

The data type

The data types in JS are divided into basic types and reference types:

  • Basic types Number, Boolean, undefined, NULL, BigInt, String, Symbol
  • Reference types Object, Function, Array, RegExp, and Date

The difference between basic types and reference types

The value of a primitive type is immutable, and the value of a reference type is mutable. When copying: Basic types copy values, reference types copy references. When comparing: Basic types only compare values. Reference types compare both reference addresses and values.

Data is stored

In js, basic types are stored in stack memory and reference types are stored in heap memory. This is because the stack access speed is quite fast, second only to the register access speed in the CPU.

Type judgment

Typeof – The method for determining the basic type

typeof "s"; // "string"
typeof { x: 1 }; // "object"
typeof [1.2.3]; // "object"
Copy the code

Tips: A historical bug with typeof

typeof null; // 'object'
Copy the code

The typeof method can only accurately determine the basic type, and the judgment for reference types is “object”.

Instanceof – Determines the reference type

Instanceof checks whether the constructor’s prototype property is present on the prototype chain of an instance object. A instanceof b, whether a is an instanceof b.

var a = { x: 1 };
var b = [1.2.3];
var c = function () {
  console.log("aa");
};
connsole.log(a instanceof Object); // true
console.log(b instanceof Array); // true
console.log(c instanceof Function); // true
Copy the code

Tips: Null and undefined cannot be detected. The instanceof method can accurately determine the data type of the reference type;

constructor

Query the constructor of an object

function getConstructor(obj) {
  let a = obj;
  return a.constructor;
}
getConstructor(1);
Logon Number() {[native code]}
getConstructor("");
ƒ String() {[native code]}
getConstructor(false);
Logboolean () {[native code]}
getConstructor(Symbol(1));
ƒ Symbol() {[native code]}
getConstructor(BigInt(1));
Logon BigInt() {[native code]}
getConstructor(function () {});
Logon Function() {[native code]}
getConstructor(new Date());
Logon Date() {[native code]}
getConstructor(/a/);
Logon RegExp() {[native code]}
getConstructor({});
Logon Object() {[native code]}
getConstructor([]);
Logon Array() {[native code]}
Copy the code

Prototype method

Object.prototype.toString.call()

Object.prototype.toString.call("xx"); // "[object String]"
Object.prototype.toString.call([12.3]); // "[object Array]"
Object.prototype.toString.call({ x: 1 }); // "[object Object]"
Object.prototype.toString.call(function () {}); //"[object Function]"
Object.prototype.toString.call(true); // "[object Boolean]"
Object.prototype.toString.call(null); // "[object Null]"
Object.prototype.toString.call(1); // "[object Number]"
Object.prototype.toString.call(undefined); // "[object Undefined]"
Object.prototype.toString.call(new Date()); // "[object Date]"
Object.prototype.toString.call(new RegExp()); // "[object RegExp]"
Object.prototype.toString.call(BigInt(2)); // "[object BigInt]"
Copy the code
  • Summarizes the data types in the js judgment method, typeof is often used to test the basic type, instanceof are often used to detect a reference type, Object. The prototype. The toString this method is a general method, also is I most like to use in the actual development.

Type conversion

The following scenarios trigger type conversions:

  • If conditional statement
  • Logical statements such as triplet expressions
  • Operators such as ==, +, -, and *

Points to note in development

  1. Number has a maximum limit:
// Access via number.max_value, beyond which calculation loses precision
Number.MAX_VALUE / / 1.7976931348623157 e+308
Copy the code
  1. Binary to decimal:
parseInt('1010'.2) / / 10
parseInt('a'.12) / / 10
Copy the code
  1. Decimal to binary:
(10).toString(2)  / / "1010"
(10).toString(12) // "a"
Copy the code

Wow, more wonderful knowledge! Get up, girls!