Number data type

  • Positive, zero, negative, decimal
  • NaN: Not a significant number, but of type number
  • Infinity: the value of Infinity, also of type number

Note that NaN is not equal to any value (including itself)

console.log(NaN= =NaN) // =>false 
Copy the code

So, how do you verify that a number is a significant number?

IsNaN is used to check whether a value is a significant number

console.log(isNaN(1)) // =>false 
console.log(isNaN(NaN)) // =>true 
console.log(isNaN(Infinity)) // =>false 
// If the value is a non-data type, it needs to be converted to a numeric type before testing
console.log(isNaN("1")) // =>false
Copy the code

Converts other data types to numeric types

  • Number([value]) isNaN checks use this method to convert to a Number type
  • parseInt([value])
  • parseFloat([value])

Number is a built-in JS conversion method that can “force” other data types to numeric types

  • String to number: An invalid numeric character occurs in the string, resulting in NaN
  • Boolean converts to numbers: true converts to 1 and false converts to 0
  • Null converts to a number: null converts to 0; undefined converts to NaN
  • The Symbol type cannot be converted to a number or an error will be reported
  • Objects are converted to numbers

Ordinary objects

  1. Let’s convert obj to the string “[object object]”
  2. Convert string to Number(“[object object]”)
let obj = {x:10}
console.log(Number(obj)) // => NaN
Copy the code

The array object

  1. Let’s convert Arr to the string “10,20”
  2. Convert “10,20” to a number :NaN
arr = ["10"."20"]
console.log(Number(arr)) // => NaN
console.log(Number([])) // => [] -> '' Number('')->0
console.log(Number(['AA'])) // => ['AA']->'AA' Number('AA')->NaN
Copy the code

The rest of the object format is essentially numeric NaN

  • Functions are converted to numbers: the result is NaN

parseInt/parseFloat

They convert a string to a number (if the value is not a string, you need to convert it to a string and then convert it to a number).

ParseInt: Searches from the leftmost part of the string, converts the found valid numeric characters to digits, and ends the search when an invalid numeric string is encountered.

console.log(Number('12px'));// NaN
console.log(parseInt('12px'));//  12
console.log(parseInt('12.5 px.));//  12
console.log(parseFloat('12.5 px.));//  12.5
console.log(Number(true));/ / 1
console.log(parseInt(true));// First convert true to the string 'true', equivalent to parseInt('true') NaN
Copy the code

ToFixed: Reserve N digits after the decimal point (the final result is a string)

let n = 3.1415926;
console.log(n.toFixed(2));/ / "3.14"
Copy the code

ES6 provides a new basic data type, BigInt: Manages numbers that exceed safe values

String data type

All strings enclosed in single/double/backquotes in JS are strings

Converts other types to strings

+ String([value])
+ [value].toString()
Copy the code

Ordinary objects are converted to strings as “[oject Object]”, array objects are converted to strings as “first item, second item” (each item in the array is separated by commas)

In addition to addition, the other cases are mathematical operations, plus in JS both mathematical operations, but also has the meaning of string splicing (as long as there is a string on either side of the plus sign, it becomes string splicing)

console.log(1 + "1");/ / "11"
console.log(1 + {});// "1[object object]"; // "1[object object]"
console.log(1 + []);/ / "1"
console.log([10] + true);When converting [10] to a number, it is first converted to the string "10", at which point the operation becomes string concatenation
Copy the code

Implicit conversions are all calls to the objCT object’s prototype methods valueOf and toString

Boolean data type

How do I convert other data types to Booleans

Boolean([value]) ! [value] Converts the specified value to a Boolean type. [value] returns negation and negation, equivalent to no negation, but only to a Boolean value typeCopy the code

Rule: only “0/NaN/null/undefined/ empty string” ends with false, all others are true

console.log(!! -1);// true
console.log(!!0);// false
console.log(!!undefined);// false
console.log(!!Number('12px'));// NaN -> false
console.log(!! []);// true
console.log(!!' ');// false
console.log(!! {});// true
Copy the code

Object Indicates the data type of an object

All objects have the following characteristics:

1. Use {key:value, commonly known as attribute name and attribute value} to describe the characteristics of an object (each object is a complex, there are zero to multiple groups of key:value,... } Each group of key-value pairs is in the format of key:value, separated by commas. 3. Key cannot be a reference data type and value can be any data typeCopy the code

Add, delete, change and check objects

1. Add or modify the attribute name and value

Object attribute names (keys) are not allowed to be repeated. If there is no previous attribute, it is added, and if there is a previous attribute, it is modified

There are two ways to manipulate objects: 1) objects. Attribute name = attribute value 2) Object [attribute name] = attribute valueCopy the code

2. Obtain the attribute name and value of the object

2) If the specified property does not exist, the value of the property is undefined(no error). 3) Get all property names in the current object: the result is an array containing all property namesCopy the code
console.log(Object.keys(obj));
Copy the code

3. Delete the specified properties of the object

1) False delete: the current property exists, but the value of the property is null. 2) True delete: the property is completely removed from the object delete.obj.nameCopy the code

The attribute name of an object cannot be a reference data type value

The way the object [property name] operates is by making sure that the property name is a value (string, number, Boolean, etc.). If it is a variable instead of a value, it will operate on the stored value as the property name of the object

Object. The operation of the property name, which follows the dot

let n = 100;
let obj = {};
obj.n = 200; // {n:200} n is the attribute name (data format is string)
obj['n'] = 200; // {n:200} n is the attribute name (data format is string)
obj[n] = 200; // {100:200} n is itself a variable (the difference between n and 'n' : the former is a variable, the latter is a string value), which represents the stored value 100(a numeric format).
obj[m] = 200; The m variable is not defined
console.log(obj);
Copy the code
 let n = {
     x:100
 };
 let m= [100.200];
 let obj = {};
 obj[n] = "Front end";{"[object object]":" front end "}
 obj[m] = "Learning";{"100,200":" learn "}
 console.log(obj);
Copy the code

If the attribute name must be an object, it can only be handled based on the new data structure Map in ES6