JavaScript data types and type conversions

Source: Internet
Author: User
Tags binary to decimal

JavaScript data types

Boolean (Boolean), number (numeric), string (String), Undefined (undefined), null (Null object), object (object type), Function

Reference type: object, array, function.

raw Data type: numeric, Boolean, NULL, string

variables of the original type cannot add members dynamically:
<script type= "Text/javascript" >        var n= ' I'm so Handsome ';        N.age=100;        alert (n.age); // The result is undefined    . </script>
objects of reference types can add members dynamically:
<script type= "Text/javascript" >        var n=New String (' Sean ');        N.age=100;        alert (n.age); //Results are  -    </script>

The variable is not the object to see if there is a new (so less rigorous).

A variable declares that there is no initial value so the result is undefined. belongs to an unknown state

A variable is assigned a value of NULL. Represents an object that does not exist.

typeof

typeof used to view the types of variables

<script type= "Text/javascript" >varn1=10; varN2=true; varn3= ' Hello '; varN4; varn5=NULL; varn6=NewObject (); Alert (typeofN1);//This can be done, but it is easy to lose a single space, causing errorsAlerttypeof(n2));//BooleanAlerttypeof(n3));//stringAlerttypeof(N4));//undefinedAlerttypeof(N5));//type is displayed as Object, why, null represents an empty object, object is of typeAlerttypeof(N6));//ObjectAlert (String (N5));//use string to display the N5 in the form of strings. If n5=null, then this object cannot call any method. </script>
instanceof

Instanceof Determines whether an object is a reference type

<script type= "Text/javascript" >        var time=New  Date ();         instanceof Date); // true        instanceof Object); // true,date are object objects    </script>

Small case:

<script type= "Text/javascript" >varS1= ' I'm not handsome '; varS2=NewString (' I'm really not handsome ')); Alert (S1instanceofString);//False,s1 is the original type, and the instanceof front should be a reference type variableAlert (s2instanceofString);//true,new out of a string object that must be trueAlert (S1instanceofObject);//false,instanceof should be a reference-type variable beforeAlert (s2instanceofObject);//true, regardless of the type of object, that belongs to typeAlert (typeofS1);//The string type, why it is a lowercase string, just display in the form of a character. AlerttypeofS2);//object type, one of new objects</script>
Type conversions

parseint (ARG) converts the specified string to an integer
Parsefloat (ARG) converts the specified string into a floating-point number
Number (ARG) converts the given value (any type) to a digit (which can be an integer or a floating point), and the entire value, not the partial value, is converted. If the string cannot be fully converted to an integral type, Nan is returned. (Not a number)
IsNaN (ARG), determines whether ARG is a non-numeric (Nan), and Nan is not equal to Nan.
String (ARG) converts the given value (any type) to a string;
Boolean (ARG) converts the given value (any type) to a Boolean type;

<script type= "Text/javascript" >varN1= ' 10 '; Alert (parseint (N1)+10);// -        varN2= ' 10ASD ';//can be identified as the number tenAlert (parseint (n2) +10);// -        varn3= ' ADSAS10JK ';//not recognizedAlert (parseint (n3) +10);//NaN        varn4= ' 321QQW ';//As above , numbers begin with letters that are recognized as numbers, and numbers are not recognized at the beginning of a letter. Alert (parseint (n4,8));//The binary conversion, converting octal to decimal, converting 8 to 16, which translates from 16 binary to decimal        varx1= ' 3.1415926KKSD ';//As above , numbers begin with letters that are recognized as numbers, and numbers are not recognized at the beginning of a letter. Alert (parsefloat (x1) +1);//4.1415926, convert to floating point        varnum1= ' 99 '; Alert (number (NUM1)+1);// -        varNum2= ' 99a '; Alert (number (NUM2)+2);//NaN        varnum3= ' a99d '; Alert (number (NUM3)+3);//NaNAlert (Number (false));//0Alert (Number (true));//1Alert (number (' 1.2.3 '));//NaNAlert (Number (NewObject ()));//NaNAlert (Number (NewDate ()));//1433944956084Alert (Number (NewDate (). tolocaletimestring ()));//NaN        //the test learned that number () is more rigorous and does not identify         //Nan, because the conversion may occur Nan, make a decision before use is not Nan        varn8= ' 12a '; if(IsNaN (Number (n8))) {//isNaN used to determine not a number .Alert (' Yes, sure is not a number ');//as a result, the alert triggers}Else{alert (' Yes, it's a number '); }        //nan and Nan are not equal        varn9= ' 12a '; varm9= ' Ads2 '; Alert (number (N9)==number (M9));//false                //String (ARG) to convert the given value (any type) to a string        vars1=100; Alert (String (S1));// -        varS2=NULL; Alert (String (S2));//NULL        varS3; Alert (String (S3));//undefined                 //String () is equivalent to the ToString () method of the calling object.         //Special column: When the object is null or undefined, the call to ToString () causes an error, and string () can be converted successfully.         vars4=undefined; Alert (s4.tostring ());//Error        vars5=NULL; Alert (s5.tostring ());//Error         varb= ' ads ';        Alert (Boolean (b)); //Boolean (ARG) converts the given value (any type) to a Boolean type</script>

JavaScript data types and type conversions

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.