What are the data types? How to judge? How to convert?

Posted by dicefile on Sun, 06 Mar 2022 12:31:11 +0100

data type

It is divided into simple type (basic type) and complex type (reference data type)

Basic types: String, number, Boolean, Null, Undefined, Symbol(ES6 added).

Complex types: object, array, function.

Note: both Array and Function are subclasses of object, so there is only one kind of object in the strict sense of complex type, which can be determined by using instanceof object

console.log(Function instanceof Object);  //true
console.log(Array instanceof Object);  //true

Symbol is a new simple data type of ES6, which means unique

Data type judgment

Simple data

typyof can directly detect simple data types, but null will be recognized as object because the tail number of the machine code is 000, but null is 00000000, so it is incorrectly recognized as object, which is a language bug.

let a = 123
let b = '456'
let c = true
let d = [7, 8, 9]
let e = { a: 1, b: 2, c: 3 }
let f = function(){}

console.log(typeof (a));  //number
console.log(typeof (b));  //string
console.log(typeof (c));  //boolean
console.log(typeof (d));  //object
console.log(typeof (e));  //object
console.log(typeof (f));  //function
console.log(typeof (undefined)); // undefined
console.log(typeof (null));   //object

Note: typeof can correctly judge the type of function, and instanceof Function can also be used to judge whether it is a function. The second one is recommended, which has reached unity

console.log(typeof (f));  //function
console.log(f instanceof Function); //true

Complex data type

function

As mentioned above, function can be judged correctly by typeof, but in order to unify the judgment methods of simple and complex types, instanceof is recommended

array

console.log(d instanceof Array) //true
console.log(Array.isArray(d)) //true

object

When we use instanceof to get an object, we need to exclude that it is not an array or function through the above two methods

if(e instanceof Object){
    if(e instanceof Array){console.log('Is an array')}
    if(e instanceof Function){console.log('It's a function')}
    console.log('Is the object')
}

About instanceof

instanceof itself means whose instance it is, so it can be used to judge whether it is an instance of a constructor

The objects created with the new keyword are actually instances, that is, they are all instances of objects in the end

function Person() { };
console.log(new Person() instanceof Person); //true
console.log(new Person() instanceof Object); //true

console.log(new Date() instanceof Date;//true
console.log(new Date() instanceof Object);//true

instanceof determines whether it is an instance of a by looking up the constructor on the prototype chain. Because of the prototype chain, it keeps looking up and finally finds the Object. Both Person and Date above are constructors.

data conversion

Explicit conversion

  1. Number(): receive a parameter: 123, true,false,null,'123' can handle other parameters, all of which are NaN
  2. parseInt(): two parameters: object + base, 123, '123','123a', the others are NaN;
  3. parseFloat(): one parameter: 123, '123.11', '123.1a', the others are NaN;
  4. String(): receiving a parameter: receiving any parameter is equivalent to + '';
  5. obj.toString(): receive a parameter: hexadecimal
  6. Boolean(): receive a parameter: '', NaN, null, undefined, 0 is false, others are true;

Implicit conversion

  1. ++/--Self increasing and self decreasing
Will use first`number()`Processing, increase or decrease in.
  1. ==
1.undefined be equal to null
2.When comparing a string with a number, the string is converted to a number
3.When the number is Boolean comparison, Boolean is converted to number
4.When comparing strings and Booleans, they are converted to numbers
  1. / % * - +
1.String plus number,The number is converted into a string.
2.Number minus string, string to number. If the string is not a pure number, it will be converted to NaN. The same is true for strings minus numbers. The subtraction of two strings is also converted into a number first.
3.The same goes for the conversion of multiplication, division, greater than, less than and subtraction.
  1. Implicit transformation law in logical operators
Logical non(!) adopt Boolean()Function converts its operand value to a Boolean value and negates it.

Logic and(&&)Operator: if the first value passes through Boolean()Function converted to true,Returns the second operation value, otherwise returns the first operation value. If one operation value is null This returns null,If one operation value is undefined,Then return undefined,If there is a value of NaN,Then return NaN. 

Logical or(||)Operator: if the first value passes through Boolean()Function conversion to false,Returns the second operation value, otherwise returns the first operation value. (Note: the operation of logic operator is short-circuit logic operation: the previous condition can get the result, and the subsequent conditions will not be executed!)

It should be noted that NaN==NaN is false, because NaN is not a number and does not specify what NaN is, so the two NaN may be different. For example, one is an object and the other is a function.

Topics: Javascript