Detecting the type of data transformation JavaScript

1. The data type of detection

In JavaScript, data type detection, typeof () method / function
typeof (variable / data), the execution result is stored in the variable data type data
typeof there is a variable space syntax typeof syntax but this is not recommended easy to program execution error
typeof not very accurate representation of each data type, generally use Boolean value to determine the type of string class
type undefined type of
other data types, then we have a more accurate, but complicated way to judge
the so-called method / function, now refers, JavaScript function defined for us

   整数,浮点数,NaN 执行结果都是 number
 		var int1 = 100;
        var float1 = 100.123;
        console.log( typeof(int1) );
        console.log( typeof(float1) );
        console.log( typeof(NaN) );

   布尔类型的执行结果是 boolean
        var bool1 = true;    
        console.log( typeof(bool1) );

   字符串类型的执行结果是  string
        var str1 = '北京';
        console.log( typeof(str1) );


        undefined 执行结果 undefined
        	console.log( typeof(undefined) );

        null 执行结果 object 
       		console.log( typeof(null) );

2. Data type conversion

What is the conversion of data types, data type conversions Why should
the JavaScript program stored in the variable data type is not limited, which is in line with variable can store any
data type JavaScript syntax specification
but during the execution of the program in JavaScript , often need variable values are stored in a particular data type, not other types of data, then you need to convert the data type of
so-called data type conversion is to convert the current data type to another data type
in JavaScript data type conversion, and automatically converted into mandatory conversion
automatic conversion is a computer program that automatically converted - essential principle of automatic conversion, must be more practice to master
the forced conversion is a programmer, forced to complete the transformation

Data type conversion is the value of the current type corresponds to the current value of the corresponding type
in the computer program, a change in the data value conversion, not to be construed as an equal relation

3. Boolean automatically converted

Other types of data, automatically converted into Boolean
when executed determines if other data types are converted to the Boolean
other Boolean type into the principle of
conversion of false: 0 '' undefined null NaN case into five false
Special 0 0.00000 0.0 are considered remind
others will all be converted to true
here does not indicate right or wrong, if 0 is executed in accordance with the conversion if the Boolean type, the conversion is false
basic syntax if judgment

         if(表达式){
            执行程序
        }else{
            执行程序
        }
        if( false ){
            console.log('真');
        }else{
            console.log('假');
        }

4. automatically converted string

Other types of automatically converted into a string
when performing string concatenation, other data types will be converted to a string
performs string concatenation, the + sign on both sides of the splice should all be of type string
if other type of JavaScript programs, automatically into a string type, then perform splicing
conversion principle
Boolean to true -> string 'true'
boolean value to false -> string 'fasle'
undefined -> string 'undefined'
unll -> string 'null'
value -> the numerical analysis is converted to the corresponding pure digital string

There are three special conversion
array -> content [] is converted to a string, stitching
Object -> any object, any content, will be converted to the [object Object] content in the form of a fixed
function -> all program code into a string

5. Automatic conversion value

Other types automatically translate into value
when performing mathematical operations will trigger automatic conversion of data type
conversion principle
Boolean: to true -> 1
false -> 0
undefined: converted to NaN
null: converted to 0

String:
if the entire string, the string is purely digital, or comply with scientific notation -> converted to the corresponding values
if the content does not meet the specifications of the digital string -> into NaN

Array, object, function
if it is plus + is string concatenation results performed
in accordance with these principles into a string data type used to transform
if other forms of operation execution result is NaN

6. boolean coerced

Other data types, coerced to Boolean, is defined in JavaScript method / function
Boolean (data / variables)
conversion principles and the same principles automatically converted
to false: 0 '' undefined null NaN3
true: other data, is converted to true
boolean () this method does not change the original values stored in the variable

int = 100 var;
the console.log (Boolean (int));
the value stored int 100, converted to a Boolean value output, the conversion result is true
use Boolean () method of conversion values stored in variables, but no changes. value stored at
only the value of a variable, analyzing data acquired, re-conversion output, without altering the contents stored in the variable

7. The string type coerced

Other types of force conversion to a string

The process variables .toString (binary type)
will be forced into a string value, and may be set to binary conversion
.toString before (), the value can not be written directly, the variables must be written in the form of
binary values are used 2 816
can be set in the range of 2--36 binary value +26 10 English letters
happened is binary, at most 36 hex

var int = 100;   
     	
console.log(int.toString(2));  
将int 中存储的 100 数值,按照二进制转化,并且以字符串的形式来输出
  				 	 如果输出时, 第一个数值1 之前的 0 , 会不输出 , 对数值大小没有影响

console.log(int.toString(8)); 	八进制

console.log(int.toString(16));	十六进制
 
console.log(int.toString( ));	( )中没有输入,默认执行十进制

console.log(int.toString(9));	进制范围是2-36

Method 2 , String (variable / data)
variables or data, into a string
principle be performed automatically according to the principle of conversion
will not change the original value stored in the variable

var int = 2e5;
console.log( String(int) );

When the string concatenation, other data types will be automatically converted into a string

8. mandatory conversion value

The other data types forced into numeric type

Method. 1 , Number The (variable / value)
will be forced into other types of numeric type, the principle of the automatic conversion of selection of transformed same

        console.log( Number(true) );   // 1
        console.log( Number(false) );  // 0
        console.log( Number(null) );   // 0
        console.log( Number(undefined) );   // NaN
        console.log( Number('100') );       // 对应的数值
        console.log( Number('100.123') );   // 对应的数值
        console.log( Number('2e4') );       // 对应的数值
        console.log( Number('123abc') );    // NaN
        console.log( Number( [1,2,3,4,5] ) );                           // NaN
        console.log( Number( {name:'zhangsan'} ) );                     // NaN
        console.log( Number( function fun(){console.log('abc')} ) );    // NaN

Method 2 , the parseInt (variable / data)
is to obtain an integer part of a variable or data
from the left-side data acquisition integer content analysis

		console.log( parseInt(true) );                                    // 都是 NaN            
		console.log( parseInt(false) );                                   
		console.log( parseInt(null) );                                    
		console.log( parseInt(undefined) );                               
		console.log( parseInt( {name:'zhangsan'} ) );                     
		console.log( parseInt( function fun(){console.log('abc')} ) ); 

An array of execution, is to obtain the value that is not part of the [] part of
1,2,3,4,5 integer part after the comma comma-11 is not an integer, then the integer part will not
get the first integer value section, if there is to get, if not, the result is NaN

		console.log( parseInt( [1,2,3,4,5] ) );        // 结果是 1                      
		console.log( parseInt( [null,2,3,4,5] ) );     // 结果是 NaN 

If it is an integer of direct access, if a floating-point number in scientific notation, the integer part to obtain

		console.log( parseInt( 100 ) );          // 整数是直接获取
		console.log( parseInt( 0.0123 ) );       // 浮点数是获取整数部分
		console.log( parseInt( 3.123456e3 ) );   // 科学计数法是解析之后,获取整数部分

String is different
if it is pure digital string

		console.log( parseInt( '100' ) );         // 与数字的结果相同 
		console.log( parseInt( '0.0123' ) );      // 与数字的结果相同 
		console.log( parseInt( '3.123456e3' ) );   //3
		console.log( parseInt( '3abc' ) ); 		 //3
		console.log( parseInt( '3.123' ) );  		//现在是一个字符串,从左侧开始,找整数部分,第一个是3,
													第二个是点,点不是整数,因此整数部分就是3
        

Method. 3 , parseFloat (variable / value)
acquired float portion

		console.log( parseFloat(true) );                                    // 都是 NaN            
		console.log( parseFloat(false) );                                   
		console.log( parseFloat(null) );                                    
		console.log( parseFloat(undefined) );                               
		console.log( parseFloat( {name:'zhangsan'} ) );                     
		console.log( parseFloat( function fun(){console.log('abc')} ) );         

Value
integers, floating point, will complete acquisition

		console.log( parseFloat(100) );
        console.log( parseFloat(100.1234) );
        console.log( parseFloat(1.234567e3) );

The key is a string
from the left side of the string from parsing of floating point numbers in line with section

		console.log( parseFloat( '100' ) );         // 与数字的结果相同 
        console.log( parseFloat( '0.0123' ) );      // 与数字的结果相同 
        
        console.log( parseFloat( '3.123456e3' ) );  // 科学技术法会解析
        console.log( parseFloat( '3.1223abc' ) );        
        console.log( parseFloat( '3.123' ) );  
Released eight original articles · won praise 0 · Views 78

Guess you like

Origin blog.csdn.net/ab_dk/article/details/104784430