data type
Data types in javascript are classified into simple data types and complex data types. Today, we only learn simple data types (5)
number,string,boolean,undefined,null
Number, string, Boolean, declaration unassigned, null type
Number
JavaScript does not distinguish between integer and floating-point types and uses number to represent them.
-
Binary system
In javascript, we can use octal and hexadecimal to represent a number, in addition to the decimal 11, 22, 33 and so on.
//Our most commonly used binary system, when doing arithmetic operations, octal and hexadecimal system will eventually be converted to binary system, the computer only recognizes binary system. //10 to 1 var num = 9; var num = 29; // Numbers at the beginning of 0 enter 1 at 8. var num1 = 010; var num2 = 0121; // Octal 0-7, octal 1, octal 10 var ba = 0321; // 12 = 2*8^0 + 1*8^1 = 10 // 321 = 1*8^0 + 2*8^1 + 3*8^2 = 1+16+ 192 = 209 console.log(ba); // Hexadecimal // Numbers starting at 0x, entering 1 at 16, range 1-9A-F var num = 0xA; var num = 0x12;
-
Floating point number
Floating point numbers are decimal numbers, such as 0.1. const num = 0.1; / ****** Scientific Counting Method*/ // When a number is large, it can be expressed by scientific counting. Var num = 3E + 3; //3 times the third power of 10 var num = 2e-2;//2 times 10-2 / *** Floating Point Loss Accuracy Problem*/ // Accuracy loss may occur in floating-point arithmetic 0.1 + 0.2 = 0.30000000000000004; 0.2 + 0.2 = 0.4; // Use floating-point numbers as little as possible, and don't let floating-point numbers compare. Solution: multiply the corresponding integer by the number of digits after the decimal point; 0.2 + 0.3 ==> (0.2*10+0.3*10) / 10 = 0.5 / ****** numerical range**/ Minimum value: Number.MIN_VALUE, which is: 5e-324 Maximum: Number.MAX_VALUE, which is 1.7976931348623157e+308 Infinity 1/0 Infinity
String type
String type, using double quotation marks " perhaps ' perhaps `` Wrapped characters ``` //Double and single quotation marks must appear in pairs const str = 'hello world'; const str = "hello world"; const num1 = '13'; const num2 = '13'; /******String Length***/ //Each string has a length attribute to get the number of strings in the string const str = "akdjflksjdflk"; console.log(str.length); /****String splicing***/ 1,+Number has the function of string splicing, it can splice two strings into one string. 2,+Number has the function of arithmetic addition. It can add two numbers together. 3,If+There is a string on both sides of the number, then it is the function of spelling, if both are numbers, then it is the function of arithmetic. // The first case: string + string const a = "hello"; const b = "world"; console.log(a + b);//String splicing function // The second case: Number + Number const a = 100; const b = 100; console.log(a + b);//addition // The third case: String + Number const a = 'abc'; const b = 100; console.log(a + b);//String splicing function
Boolean type--boolean
Boolean types: true and false // Boolean types have only two values true: Represents Truth False: Represents false
undefined and null
They all fall into the category of getting abnormal values. undefined represents a variable that has no assignment Null denotes an empty value, (for example, get an element, id is written incorrectly, get no, return a null)
Judgment of data type
1. typeof
typeof returns a string representing the data type, and the results include: number, boolean, string, symbol, object, undefined, function and other seven data types, but can not judge null, array, etc.
2. instanceof
Instanceof is used to determine whether A is an instance of B. The expression is A instanceof B. If A is an instance of B, then true is returned. Otherwise, return false. The instanceof operator is used to test whether an object has a prototype attribute of a constructor in its prototype chain, but it cannot detect null and undefined
3. constructor
The constructor function is very similar to instanceof. But constructor detection Object is different from instanceof, and it can also deal with the detection of basic data types. However, the constructor of the function is unstable, which is mainly reflected in rewriting the prototype of the class. In the process of rewriting, it is possible to overwrite the previous constructor, so the results detected are inaccurate.
4. Object.prototype.toString.call()
Object.prototype.toString.call() is the most accurate and commonly used method.
Object.prototype.toString.call('') ; // [object String] Object.prototype.toString.call(1) ; // [object Number] Object.prototype.toString.call(true) ; // [object Boolean] Object.prototype.toString.call(undefined) ; // [object Undefined] Object.prototype.toString.call(null) ; // [object Null] Object.prototype.toString.call(new Function()) ; // [object Function] Object.prototype.toString.call(new Date()) ; // [object Date] Object.prototype.toString.call([]) ; // [object Array] Object.prototype.toString.call(new RegExp()) ; // [object RegExp] Object.prototype.toString.call(new Error()) ; // [object Error]