JS data type

Source: Internet
Author: User

Boolean null undefined number String Symbol (ECMAScript 2015)

The type can be detected by TypeOf.

var x = 1;        Console.log (typeof x);//number                 var a = undefined;        Console.log (typeof a);//undefined                  var b = null;        Console.log (typeof b);//object, (null is a null object reference/or pointer).                   var c = new Object ();        Console.log (typeof c);//object                    var e = [n/a];        Console.log (typeof e);//object                      var d = function () {                  //... Statement block        }        Console.log (typeof D);//function        var e = {};        Console.log (typeof e); Object        var F;        Console.log (typeof f);//undefined        var g = "HelloWorld";        Console.log (typeof g);//string        var h = true;        Console.log (typeof h);//Boolean

1.Number Type

Number type contains integers and floating-point numbers

NaN: Non-numeric type. Features: ① any operation related to Nan, will return Nan②nan not equal to itself

var ab = "A1"; Console.log (AB/10);//NaNconsole.log (Nan = = Nan);//false;

The IsNaN () function, which is used to determine whether a non-numeric type. Returns true if the passed parameter is a non-numeric type, otherwise false;

        var = num1;        Console.log (IsNaN (NUM1));    False        var num2 = "Test";         Console.log (IsNaN (num2));    True        var num3 = "666";        Console.log (IsNaN (num3));  False

The Infinity property is used to hold numeric values that represent positive infinity.

var num4 = 66/0;console.log (NUM4);//infinityconsole.log (typeof (Num4));//number    

Numeric conversions:

Number () transformation function, which can be used for any data type;

parseint (), convert the value to integer type, use more;

Parsefloat (); Converts a value to a floating-point type.

2.String type

String conversion: The transform function, string (), applies to any data type (null,undefined converted to null and undefined), and the ToString () method (null,defined does not have the ToString () method).

var BC = "Test"; var bd = Null;var be = Undefined;var ob = {};console.log (bc.tostring ()); Testconsole.log (Ob.tostring ()); [Object Object]console.log (typeof ob.tostring ()); String//console.log (Bd.tostring ()),//error error//console.log (be.tostring ()),//error error Console.log ("------"); Console.log (String (BC)); Testconsole.log (String (BD)); Nullconsole.log (BE); Undefinedconsole.log (String (ob));    

3.Null type

The null type is considered a null object pointer, and the null type is also an empty object reference. There is only one value, the null value, so when you use the TypeOf operator to detect the value of a null type, the result is the object type.

var test = Null;console.log (typeof test); Object

4.Undefined Type

There is only one value, that is, the undefined value. The variable is declared with Var, but the value of the variable is not initialized to the value of undefined.

var name = "Test"; var age;console.log (name);//testconsole.log (age);//undefined//console.log (height);//error, Variable height does not declare console.log (typeof name);//stringconsole.log (typeof age);//undefinedconsole.log (typeof height);// Undefined, variable height is not declared

5.Boolean type

True or False

var times = new Date (""); Console.log (times); Invalid Dateconsole.log (times = = null);  Falseconsole.log (Times = = "Invalid Date"); Trueconsole.log (Times = = = "Invalid Date");//Falseconsole.log (typeof times);//objectconsole.log (times.tostring () = = = "Invalid Date"); Truevar Newobj1;/console.log (NEWOBJ1);//undefinedconsole.log (typeof newObj1);//undefinedconsole.log (newObj1 = = NULL);//trueconsole.log (newObj1 = = undefined);//trueconsole.log (newObj1 = = null);//falseconsole.log (newObj1 = = = undefined);//true var newObj2 = null; Console.log (NEWOBJ2);//nullconsole.log (typeof newObj2);//objectconsole.log ( NewObj2 = = null);//trueconsole.log (newObj2 = = undefined);//trueconsole.log (newObj2 = = = null);//trueconsole.log ( NewObj2 = = = undefined);//false//0/0 5/0var tnum = 5/0; Console.log (Tnum); Infinityconsole.log (typeof Tnum);//numberconsole.log (Tnum = True);//falseconsole.log (IsNaN (Tnum));//false

JS Data type

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.