Javascript Base Point

Source: Internet
Author: User

1. Basic types

There are several basic data types for Javascript:

    1. Number
    2. String:
    3. Boolean:
    4. 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 .
    5. Null: means that the variable has no value or is empty.

Except for the above type, the other types are object

typeofreturn 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

NaNis 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
    • DigitalNaN
    • 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.