1. Js data type

Six classes, five basic data types, and one complex type (object type, reference type)

Five basic data types: Number, String, Boolean, null, and undefined

Complex type Object(Object, function, array)

Basic data types

1.1 number

Integers, decimals, octal, hexadecimal, NaN (not a number) all belong to the number type

//1.Number var n1 = 10; Var (n = 5.5; //typeof: check variable data type console.log(typeof (n1)); //number console.log(typeof n2); // number var n3 = 057; // Start with 0 and no number greater than 8, base 8 console.log(n3); //47 var n4 = 0x11; // start with 0x, hexadecimal 0-9 a b c d e f console.log(n4); //17 var n5 = NaN; //not a number console.log(10*"a"); NaN console.log(NaN==NaN); Console. log(0.1+0.2); console.log(0.1+0.2); / / 0.30000000000000004 console. The log (0.1 + 0.2 = = 0.3). //falseCopy the code
1.2 the string

String: String quotes are all String “” “”

var s1 = "str"; var s2 = '1234'; console.log(typeof s1,typeof s2); //string string var passWord = "13145672345"; Var l = password. length; console.log(l); //2.2 charAt(): Get the string subscript of the corresponding position starting from 0 console.log(password.charat (0));Copy the code
1.3 Boolean
Var b1 = true; var b1 = true; var b2 = false; console.log(10>20);Copy the code
1.4. Null is undefined
Var a; var a; console.log(a); Var b = null; var b = null; // The value is empty console.log(document.getelementbyid ("text")); //nullCopy the code

1. Null vs. undefined

Null: the value returned by accessing a nonexistent object. Null: The value returned by accessing a nonexistent object

The complex type

1.5 Complex Types Object, Array, and Function

var oDiv = document.getElementById("box"); console.log(typeof oDiv); //object console.log(typeof document); //object console.log(typeof window); Var obj = {"sex":" female ", "age":18, "height":"165"} console.log(obj); console.log(obj.height); Var arr = [1,2,true,null,"f"]; //length: count the number of arrays console.log(arr.length); console.log(arr[1]); // get the element by subscript, starting at 0. Function suibian(){console.log(" whatever "); function suibian(){console.log(" whatever "); } suibian(); suibian();Copy the code
1.6 Classification criteria for data types

[img-ISA99038-1572164658475]

Reference types are stored in the heap, and memory addresses are stored in the variable names of reference types

Var arr = [1, 2, 3]; var arr1 = arr; // Assign the memory address of the arR to arr1.push(4); // Add a value console.log(arr1) to the end of the array; //[1, 2, 3, 4] console.log(arr); / / [1, 2, 3, 4]

In the code above, arr1=arr; It is ARR that copies its memory address to ARR1,

Arr1.push (4), Arr1 finds the memory address of the array and adds 4 to the end of the array

console.log(arr); //[1, 2, 3, 4], because now share the same block address, arr is also 1234

2. Data type cast

2.1 turn number

var n = 11; var s = "10"; var b = true; var nu = null; var u; Console. log(Number(n)); //undefined //1.Number(converted value), the previous variable does not change the type, console.log(Number(n)); //11 var ss = Number(s); // Convert to 10 console.log(ss) of type number; //10 console.log(s); //"10" console.log(Number(b)); //true---1 false----0 console.log(Number(nu)); //0 console.log(Number(u)); //NaN console.log(Number("")); //0 console.log(Number("12px")); //NaNCopy the code

Note: Number can be converted, Boolean, NULL, string (“” pure numeric characters), and all else is NaN

2. ParseInt: cast to Number (integer)
3. ParseFloat: cast to Number with fractional parts reserved
console.log(parseInt("12px")); / / 12 console. The log (parseInt (" 12.5 px ")); / / 12 console. The log (parseFloat (" 12.5 px ")); Console. log(parseInt("a123")); //NaN console.log(parseInt("12a3")); //12 //parseInt(string),radix(radix)) 2-36 default 10 mechanism console.log(parseInt("1110",8)); //Copy the code

2.2 turn the string

//String(converted value) var n = 10; var s = “1”; var b = false; var nu = null; var u ; console.log(String(n)); //”10″ console.log(String(b),b); //”false” false console.log(String(nu)); //”null” console.log(String(u)); //”undefined”

ToString (radix) 2-36 console.log(n.tostring ()); console.log(b.toString()); // console.log(nu.toString()); There is no such method // console.log(u.tostring ());

​ console.log(n.toString(8));

2.3 isNaN

//isNaN: is not a number; //isNaN (12)); //isNaN: is not a number; //isNaN (12)); / / false console. The log (isNaN (” 12.5 “)); / / false console. The log (isNaN (” 12.5 px “)); //true

3. The operator

1. The arithmetic operator + – * / % (modular) ++ autoadd — autosubtract

console.log(10/4); / / 2.5 the console log (4 10%); / / 2

var a = 10; //a++; //++a; // Add console.log(++a); // Add console.log(++a);

var a = 10; var b = a++ + ++a + a++; //b = 10 + 12 + 12 =34 a = 13 var c = ++a + ++b + b++; //c = 14 + 35 + 35 =84 a = 14 b=36 console.log(a,b,c); //a=14 b=36 c=84

var x = 1; var y = 1; x++; ++y;

var s = ++x + ++y + y++; //s =3 + 3 + 3=9 x = 3 y=4 console.log(x++,y,s); //x = 3 y = 4 s = 9

2. Assign operator = += -= *= /= %=

var x = 10; x += 3; // x = x + 3; cumulative

3. Compare operator > < >= <= == equal! = unequal === congruent! = =

console.log(“10000″>”2”); // The false string is a bit – to – bit comparison

console.log(“10″==10); / / true just as numerical “10” — – > 10. The console log (” 10 “= = = 10); // False and type must be the same to be equal

4. Logical operators && | |!

var s = 60; console.log(s>=70 && s<=100); // Join two judgment conditions, two true is true

var s3 = 90; console.log(s3<60 || s3>100); // Join two judgment conditions, one true is true

Var s4 = 1 &&0; Console. log(s4); console.log(s4); console.log(s4); console.log(s4); / / 0

var s5 = 0 && 1; console.log(s5); / / 0

var s6 = (0 && 2); console.log(s6); / / 1. 2

/ / a true is true, if the first condition is true, won’t go to the second condition, the first is false, and to judge the second var t1 = 1 | | 2. console.log(t1); / / 1

var t2 = 0 || 2; console.log(t2); / / 2

var t3 = 0 || false; console.log(t3); //false

/ /! False true console.log(! (> 10 20)); / /! false =true console.log(! 0); //true console.log(! 10); //false

5. Ternary operators

/ / conditions? Var day = 1; var day = 2; day == 7 ? Console. log(” sleep until 2 PM “) : console.log(” Study hard and make progress every day “); */ var age = 7; age <= 6 ? Console. log(” Play mud at home “) : console.log(” play mud at school “);

6. Determine gender

Female male sure

Obtn.onclick = function () {console.log(girl.checked); // Check whether the first checkbox is checked, if true, print female, if not checked, print male girl.checked == true? Console. log(” female “):console.log(” male “); }

7. Implicit conversion

/ / + 101 + “age” = “101” the age of the console, log (100 + 1 + “age” + 10 + 20); //101age1020 + becomes the concatenation console.log(“”+10+20); //1020 console.log(“100″+2000); //1002000 console.log(true+false); //true—1 false—-0 1 console.log(true+”10”); //true10 console.log(true+null); //true—1 null–0 1 console.log(10+undefined); //NaN

Log (“100”-20); //- console.log(“100″-20); //”100”–100 80 console.log(“100”-“20”); //80 console.log(“100”-“20px”); //NaN console.log(100-null); //100 console.log(100-“”); / / 100

/ / by * the console log (100 * “a”); //NaN console.log(100*””); //0 console.log(100*”100″); //10000 console.log(100null); //0 console.log(100undefined); //NaN console.log(100*true); / / 100

In addition to / / / c console. The log (100 / “a”); //NaN console.log(100/””); //Infinity console.log(100/”70″); / / 1… console.log(100/null); //Infinity console.log(100/undefined); //NaN

// Take more than % /console.log(100%”a”); console.log(100%””); console.log(100%”70″); console.log(100%null); console.log(100%undefined);; /

var n = “10”; n++; // “10” —- 10 console.log(n) /* + is a concatenation when it encounters a string (as long as one of the operations is a string, it is directly concatenated)

Different types are more regular

=== : Is equal only when they are identical

== : There is conversion in the judgment process

1. Compare strings with numbers — convert strings to numbers before comparing

Compare a Boolean to a number — first convert a Boolean to a number, then compare

3, Boolean and string comparison — both converted to number, in comparison

4. Compare arrays to strings — Convert arrays to strings in comparison

5. Compare objects to strings – Convert objects to strings in comparison

NaN is not NaN

Null is equal to undefined, not equal to any other value except itself

console.log(true==1); //true console.log(true=="1"); //true--1 "1"--->1 console.log([]==""); //[]---"" true console.log({}.toString()); //[object Object] console.log({}==""); //[object Object] console.log(NaN==NaN); //false console.log(null==undefined); //true console.log(null==null); //true console.log(undefined==undefined); //true console.log(null == 0); //falseCopy the code