js-Data types

Source: Internet
Author: User

There are 5 types of data in JS: Undefined, Null, Boolean, number, and string.
There is also a complex data type Object,object essence is a set of unordered name-value pairs.

First, the data type

1. Undefinde type
The undefined type has only one value, that is, undefined, which declares the variable using VAR, but is not initialized, and this variable is of type undefined, for example:
var i;
alert (i = = undefined);//true
var i; var i = undefined; The two sentences are equivalent.
Variables that contain undefined values are not the same as undefined variables.

2. Null type
The null type also has only one value: Null.null represents a pointer to an empty object.

3. Boolean type
Boolean type: Only two literal literals are true and false. However, many variables in JS can be converted to a Boolean value using the Boolean () function.

<script type= "Text/javascript" >var J;console.log (j+ ":" +boolean (j));//falsevar J=null;console.log (j+ ":" + Boolean (j)); Falsej=50;console.log (j+ ":" +boolean (j));//truej=0;console.log (j+ ":" +boolean (j));//falsej= "0"; Console.log (j+ " : "+boolean (j));//truej=" ", Console.log (j+": "+boolean (j));//falsej=" abc "; Console.log (j+": "+boolean (j));// Trueconsole.log ("----------------------"); Console.log ("null==": "+ (null=="));//falseconsole.log ("null== ' null ') : "+ (null== ' null ')"),//falseconsole.log ("null==undefined:" + (null==undefined));//truealert (123==123); alert (0== true);//falsealert (true== "true");//falsealert (false==0);//truealert (1+1==2);//truealert (0===true);//falsealert (false===0);//false</script>


4. Type of number

Integers and floating-point numbers. Nan:not A number (not a numeric type of numeric type). This value is used to return a value, but it is not possible to put back a value to prevent an error. For example, 1/0 returns Nan.

Features of Nan:

1. Any operation that involves Nan will return Nan.

2. Nan is not equal to any value, including its own Nan itself.
For Nan features, JS built-in isNaN () function, to determine whether a variable is a number. If it is a number, returns false, not all numbers, returns true
5. String type

Usually enclosed in double or single quotes, all of them are of type string.

<script type= "Text/javascript" >var I;console.log (i+ ":" +string (i));//undefinedi=null;console.log (i+ ":" + String (i));//nulli=123;console.log (i+ ":" +string (i));//123i=true;console.log (i+ ":" +string (i));//true</script >

Second, typeof operator

    The type of the inferred variable for a variable may return the following string:
"Undefined" if this value is undefined or initialized
"Boolean" Boolean value
"String" string
"Number" value
"Object" objects
"Function" functions
Usage: typeof 95; or typeof (95); will return "number".

    

<script>var b=function str () {var a=100;alert (a);}; Console.log (b + ":" +typeof "), var i=100;console.log (i+": "+typeof (i)), I=1.2;console.log (i+": "+typeof (i)); i= 1.2345678;console.log (i+ ":" +typeof (i)) I=-1.23;console.log (i+ ":" +typeof (i)); I=1.2e8;console.log (i+ ":" +typeof (i ); I=034;console.log (i+ ":" +typeof (i)), I=0x12c;console.log (i+ ":" +typeof (i)); i= "123"; Console.log (i+ ":" +typeof (i) ); I=true;console.log (i+ ":" +typeof (i)), I=false;console.log (i+ ":" +typeof (i)), Var j;console.log (j+ ":" +typeof (j)); =null;console.log (i+ ":" +typeof (i)), var str= "123abc"; alert (typeof (str++)); alert (str);</script>

iii. Conversion of data types:

1. Forcing conversions of data types: You know what data types the variables will be converted to

Force data type conversion
            1, number (): Forced to change the variable to a numeric type
   & nbsp;            nan: Numeric type that is not a numeric type   not a number
            2, parseint (): Forces a variable to a numeric type, and converts to an integer
             parsefloat (): Forced to change the variable to a numeric type, and convert to float single-precision floating-point

<script type= "Text/javascript" >//alert (typeof (NaN)), Var a;console.log (A + ":" +number (a)); A=null;console.log ( A + ":" +number (a)), "A=",//alert ("+100"), Console.log (A + ":" + (number (a) +100)), a= " -100", Console.log (A + ":" + ( Number (a) +100), a= "", Console.log (A + ":" + (number (a) +100)), A=true;console.log (A + ":" +number (a)); a=false; Console.log (A + ":" +number (a)), a= "123abc", Console.log (A + ":" +number (a)), Var i= "123"; Console.log (i+ ":" +parseint (i)) var i= "123.456", Console.log (i+ ":" +parseint (i)), var i= "123.abc", Console.log (i+ ":" +parseint (i));//nanvar i= "A123BC "; Console.log (i+": "+parseint (i)), var i=" 12A3BC ", Console.log (i+": "+parseint (i)), Var i=0x10a;console.log (i+": "+ parseint (i));//force convert 16 binary to decimal var i=123.0;console.log (i+ ":" +parsefloat (i));</script>

2. Conversion of implicit data types: You don't know what type of data the variable will be converted to, but the data does convert

Conversion of an implicit data type:
into a string: +
Become a number:-*/%
Become a number: + +--
Comparison of numbers, Comparison of strings:> <
Convert the data type on the right to the Boolean type:!

<script type= "Text/javascript" >var i= "", Console.log (i+100); Console.log (100-"3"); Console.log (100*3); Console.log (100/3); var i= "3"; I++;console.log (i);//auto-Add and auto-subtract will automatically convert to Number//alert ("3" >2);   True//alert ("3" > "2");  True if (Boolean (2)) {alert ("AAAA");} </script>

js-Data types

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.