"Summary" JavaScript basics

Source: Internet
Author: User

1. JavaScript data types
    • Number
    • String
    • Boolean
    • Undefined
    • Null
    • Object

For example, whereNumberStringBooleanUndefinedNullIs the original type,ObjectIs the object type,ObjectCan containArrayFunctionand other types.

2. General operators

Let's look at a few lines of code

1 var num=322var str=3// Use this to convert a number to a string of 4  var num=1235 num=num+ ""//" +"
num= ""num=num-0// convert to digital

When the left and right sides of the regular operator areNumberAndString, if it is a plus, it willNumberTurn intoStringProcessing, if it is a minus sign, thenStringwill be turned intoNumberProcessing, multiplication, and division are similar to subtraction.

3, equal to and strictly equal to
var c= (0==false)/truevar c= (0===false)//  false strictly equals value equal except for type equality 
(NULL==undefined)//true(NULL===undefined)//falseNewObject () = =NewObject ()//False object is a reference type[1,2]==[1,2]//false because the array is an object so it is falseNULL==NULL  //trueNULL===NULL  //trueundefined==undefined//trueundefined===undefined//trueNaN==nan//falseNan===nan//false nan and nobody wants to wait, even if it's their own12== "12"//true equals one on either side is a number, one is a string, and the compiler tries to convert the string to a numbertrue==1//true equals one on either side is a Boolean and one is a different type, and the compiler tries to turn the Boolean into a number and then comparetrue= = "1"//truevarA=NewObject ("123") A==123//trueA= "123"//trueIf the equal sign is on either side of the other side of object is number or String,object will be converted to base type then compare
4. Type packing

The original type,,, String Number have the Boolean wrapper type

var str= "string"var s=New//String wrapper class, contains many methods and properties str.t=10//  str is a basic type, but when we attach a property to STR and assign a value, JavaScript intelligently converts STR to the corresponding temporary wrapper type, and when it accesses the T attribute of Str again, the value is undefined, Because the generated temporary wrapper object is destroyed, the value of T becomes undefined. 
5. Type detection

There are 5 main types of detection methods

    • Typeof
    • instanceof
    • Object.prototye.toString
    • Constructor
    • Duck type
typeof100//"Number"typeof true//"Boolean"typeof function(){}//"function"typeof(undefined)//"undefined"typeof NewObject ()//"Object"typeof[Up]//"Object"typeofNaN//"Number"typeof NULL//"Object"? It would have been a rule to return "NULL" but this resulted in a large number of Web sites being paralyzed, so the provision returned "Object"typeofNull//"undefined"

nullThe returned type is not the same when the case is inconsistent

typeofSuitable for the original type and function the detection, encountered the null failure

instanceofis based on the prototype chain, applicable to Object custom objects, can also be used to detect native objects, in different iframe and window detection, the right object must be a function object or a function constructor, or throw an exception

Object.Prototype.toString.appy ([]) obtained through {}.tostring, suitable for built-in objects and native types, encountered null and undefined invalidated

Instanceof 123//will throw the following exception
Uncaught syntaxerror:unexpected Token instanceof

[instanceof] Array//true!!! The principle is to determine if there is a prototype property of the right constructor on the prototype chain of the left object.

Ject. Prototype.toString.appy ([]); = = ="[Object Array]" Object.Prototype.toString.appy (function() {}); = = ="[Object Function]" Object.Prototype.toString.appy (NLL); // IE 678 Returns a [Objet object] compatibility issue Object.Prototype.toString.appy (undefined); = = = "[Object undefined]"

"Summary" JavaScript basics

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.