1. Basic types
There are several basic data types for Javascript:
- Number
- String:
- Boolean:
- Undefined: When you access a non-existent variable, you get it
undefined
. It is also available when you define a variable but do not assign it a value undefined
. JavaScript automatically initializes the variable to that value, and the undefined
type has only one value undefined
.
- Null: means that the variable has no value or is empty.
Except for the above type, the other types are object
typeof
return value
- "Number"
- "String"
- "Boolean"
- "Undefined"
- "Object"
- "Function"
Numeric prefixes
// Octal, hexadecimal numbersvar0377; // octalvar0x9F; // hex// Exponent literalsvar2e3; // 2000var2e-3; // 0.002
Infinity
Infinity is of type number, and JavaScript can handle a maximum value of 1.7976931348623157e+308 and a minimum of 5e-324.
varA =6/0;//Infinityvari =-Infinity;//-infinityvarm =Infinity–Infinity;//NaNvarn =-Infinity+Infinity;//NaNvarN1 =Infinity– -;//InfinityvarN2 =-Infinity*3;//-infinityvarN3 =Infinity/2;//InfinityvarN4 =Infinity–99999999999999999;//Infinity
NaN
NaN
is also a special number value:
vartypeofNaN; // "number"varNaN; // NaNvar10"f"; // NaNvar12NaN// NaN
2. String type Conversion
var‘1‘3 * s;typeof s;// "number"s;// 3var‘1‘;s++;typeof s;// "number"s;// 2
3. Logical Judgment
Any value can be given a Boolean value with two backslashes. In addition to the following several return false, most values return true:
- Empty string ""
- Null
undefined
- Number 0
- Digital
NaN
- Boolean false
Note the string "0", "", and "false" to true when converting to a Boolean value
4. Comparison characters
1==1;//True1==2;//False1==' 1 ';//True1===' 1 ';//False1===1;//True1!=1;//False1!=' 1 ';//False1!=' 2 ';//True1!==1;//False1!==' 1 ';//TrueNaN==NaN;//False//NaN is not equal to any value, including his own
Undefined and Null
Foo//Referenceerror:foo is not definedtypeofFoo//"undefined"varSomevar;somevar;typeofSomevar;//"undefined"//JavaScript automatic initialization variable value is undefinedvarSomevar;somevar = = =undefined;//True//JavaScript does not automatically assign null initial values to variablesvarSomevar =NULL;//nullSomevar;//nulltypeofSomevar;//"Object"//undefined & nullvari =1+undefinedI//NaNvari =1+NULLI//1//Convert to a number:1*undefined;//NaN1*NULL;//0//Convert to a Boolean value:!!undefined;//False!!NULL;//False//Convert to a string:"Value:"+NULL;//"Value:null""Value:"+undefined;//"value:undefined"
5. Array Arrays
varA = [1,2,3];//You can add items to the array in the following waysa[3] =' Four ';//"four"//If you add an item this way, items that were not specified before this index will be initially undefineda[6] =' new ';//"new"A//[1, 2, 3, "Four", Undefined, Undefined, "new"]//Use Delete to remove itemsvarA = [1,2,3];Deletea[1];//TrueA//[1, Undefined, 3]typeofa[1];//"undefined"the//array can contain multiple types of data and can even contain another arrayvarA = [[1,2,3], [4,5,6]];a;//[array[3], array[3]
6. Code blocks check for variable existence
varresult ="";if(typeofSomevar!=="undefined") {result ="Yes";} Result// ""varSomevar;if(typeofSomevar!=="undefined") {result ="Yes";} Result// ""Somevar =undefined;if(typeofSomevar!=="undefined") {result ="Yes";} Result// ""Somevar =123;if(typeofSomevar!=="undefined") {result =' yes ';} Result//"Yes"
Javascript Base Point