Data types-learn that you are invincible!

Posted May 27, 20202 min read

Basic data types
The basic data types are:undefined, boolean, number, string, null The basic types of access are accessed by value, which means you can manipulate the actual value stored in the variable.

Reference data type
The reference data type of js is the object type Object, such as:Object, array, function, data, etc .;

How to distinguish what type

1. Use the typeof operator.

(1) undefined:If this value is undefined

(2) boolean:If this value is a boolean

(3) string:If the value is a string

(4) number:If this value is a number

(5) object:If the value is object or null

(6) function:If this value is a function

Note:typeof is not suitable for judging whether it is an array. When using typeof to judge arrays and objects, both will return object.

You can use isArray() to judge whether it is an array.

    var str = ""
    var str1 = typeof(str);

    console.log(str1) //"string"

2.instanceof

The instanceof operator is used to determine whether the object pointed to by the prototype property of a constructor exists on another prototype chain of the object to be detected. Need to be case sensitive.

In simple terms, instanceof is used to judge whether a variable is an instance of an object.

    var arr = new Array();
    console.log(arr instanceof Array); //return true

It should be noted that instanceof can only be used to judge objects and functions, not strings and numbers. When determining whether it is a string or a number, it will only return false

3. constructor

The constructor property returns a reference to the array function that created this object.

In JavaScript, every object with a prototype will automatically get the constructor attribute.

\ [Native code ]in the following code means that this is JavaScript's underlying internal code implementation and the code details cannot be displayed.

//String
var str = "String";
alert(str.constructor); //function String() {\ [native code ]}
alert(str.constructor === String); //true //Array
var arr = \ [1, 2, 3 ];
alert(arr.constructor); //function Array() {\ [native code ]}
alert(arr.constructor === Array); //true //Number
var num = 5;
alert(num.constructor); //function Number() {\ [native code ]}
alert(num.constructor === Number); //true

4, prototype

The above three methods will have some cases that cannot be judged. To ensure compatibility, you can use the Object.prototype.toString method to determine which built-in type an object value belongs to.

Needs to be case sensitive.

alert(Object.prototype.toString.call("String") === \ [object String ]) -------> true;
alert(Object.prototype.toString.call(123) === \ [object Number ]) -------> true;
alert(Object.prototype.toString.call(\ [1,2,3 ]) === \ [object Array ]) -------> true;
alert(Object.prototype.toString.call(new Date()) === \ [object Date ]) -------> true;
alert(Object.prototype.toString.call(function a() {}) === \ [object Function ]) -------> true;
alert(Object.prototype.toString.call({}) === \ [object Object ]) -------> true;


Editor is not easy, if there is any harvest, WeChat rewards editor to drink Wahaha
image.png

Single dog's sunflower collection, a must-have for sisters, stay tuned!
image.png