• 数值转换题


    一,  请写出下列表达式的值
          var a = 5;      var b = 0;
          (1)"" + a - b;    _____5_______            
          (2)typeof ("" + a - b);    ___number________		
          (3)Boolean(a) - b;    _____1______
    
    
         var n = null;	 var m;
          (4) typeof(n);     ____object_____			
          (5) typeof(m);    ____undefined_____	 		
          (6) Number(n) + Number(m); _____NaN_______
          (7) Boolean(n) - Boolean("n");  _____-1_______ 	
          (8) Boolean(m) - Boolean(n);  _____0________		
          (9) m--;      ____NaN_______
          (10) +n;     _____0______
    二  填空题
         (1) 数组中可以添加元素,但是不会修改原数组的方法是 ____concat______, 可以向数组中的任意位置添加元素的方法是____splice______
         (2) 只能以__字母____,  __下划线_____,  __$_____作为变量名的开头
         (3) 数组的三要素 : __下标_____,  ___元素____, __数组长度______
         (4) 结束循环语句的关键字是___break_____, 结束本轮循环的关键字是 ___continue______
         (5) 假如price = 900,    "price > 500 && price *= 0.8" 这句代码的运行结果是__720_____
    
    逻辑与的短路运算 : 
    var res = undefined || null || 0 || 10 || 1 || '';
    console.log ( res );
    	        10
    var res =  100 && null && 0 && 10 && 1 && '';
    console.log ( res );
    		null
    var age1 = 10;
    var age2 = 10;
    var res1 = typeof(age1) && age1 + age2;		20
    var res2 = typeof(age1) || age1 + age2;		10
    var res3 = (age1 - age2) && age1 +age2;		0
    var res4 = (age1 - age2) || age1 +age2;		20
    
    var  c = 12;
    var res1 = " " && c;	12
    var res2 = "NaN" && c;			12
    var res3 = undefined || c + "c";	12c
                                                                                                console.log ( Number ( "123.1.1abc" ) );  NaN
    console.log ( parseInt ( "123.1.1abc" ) );   123
    console.log ( parseFloat ( "123.1.1abc" ) );  123.1
    
    console.log ( Number ( false ) );  0
    console.log ( Number ( undefined ) );  NaN
    
    
    console.log(+null);  0
    console.log(+"false");  NaN
    console.log(+false);    0
    console.log(+undefined);  NaN
    console.log(+"number");  NaN            
    
        var a = 5;
        var b = 0;
        console.log ( a+b );			5
        console.log ( typeof (a+b) );		number
        console.log ( a-b );			5
        console.log ( typeof (a-b) );		number
        console.log ( a-Boolean(b) );		5
        console.log ( typeof (a-Boolean(b)) );	number
        console.log ( Boolean(a)-b );		1
        console.log ( typeof (Boolean(a)-b) );	number
    
    
        console.log ( Boolean ( 0 ) );		flase
        console.log ( Boolean ( 0.0 ) );		flase
        console.log ( Boolean ( "0" ) );		true
        console.log ( Boolean ( null ) );		flase
        console.log ( Boolean ( "null" ) );		true
        console.log ( Boolean ( false ) );		flase
        console.log ( Boolean ( undefined ) );	flase
        console.log ( Boolean ( ' ' ) );		true
        console.log ( Boolean ( NaN ) );		flase
        console.log ( Boolean ( "false" ) );	true
        console.log ( Boolean ( "undefined" ) );   	true  
    
    转成数字类型
    parseInt(".123abc");  NaN
    parseFloat(".12.3abc");  0.12
    parseInt(123);   123
    parseFloat(123);  123
    parseInt(123.25);  123
    parseFloat(123.25); 123.25
    
    console.log ( Number ( "123" ) );   123
    console.log ( Number ( "123.1.1abc" ) );  NaN
    console.log ( parseInt ( "123.1.1abc" ) );   123
    console.log ( parseFloat ( "123.1.1abc" ) );  123.1
    
    console.log ( Number ( false ) );  0
    console.log ( Number ( undefined ) );  NaN
    
    
    console.log(+null);  0
    console.log(+"false");  NaN
    console.log(+false);    0
    console.log(+undefined);  NaN
    console.log(+"number");  NaN
    
    
    
        var a = 5;
        var b = 0;
        console.log ( a+b );			5
        console.log ( typeof (a+b) );		number
        console.log ( a-b );			5
        console.log ( typeof (a-b) );		number
        console.log ( a-Boolean(b) );		5
        console.log ( typeof (a-Boolean(b)) );	number
        console.log ( Boolean(a)-b );		1
        console.log ( typeof (Boolean(a)-b) );	number
    
    
       console.log ( Boolean ( 0 ) );		flase
        console.log ( Boolean ( 0.0 ) );		flase
        console.log ( Boolean ( "0" ) );		true
        console.log ( Boolean ( null ) );		flase
        console.log ( Boolean ( "null" ) );		true
        console.log ( Boolean ( false ) );		flase
        console.log ( Boolean ( undefined ) );	flase
        console.log ( Boolean ( ' ' ) );		true
        console.log ( Boolean ( NaN ) );		flase
        console.log ( Boolean ( "false" ) );	true
        console.log ( Boolean ( "undefined" ) );   	true    
    
    console.log ( Number ( "123" ) );   123
    console.log ( Number ( "123.1.1abc" ) );  NaN
    console.log ( parseInt ( "123.1.1abc" ) );   123
    console.log ( parseFloat ( "123.1.1abc" ) );  123.1
    
    console.log ( Number ( false ) );  0
    console.log ( Number ( undefined ) );  NaN
    
    
    console.log(+null);  0
    console.log(+"false");  NaN
    console.log(+false);    0
    console.log(+undefined);  NaN
    console.log(+"number");  NaN
    
    
    
        var a = 5;
        var b = 0;
        console.log ( a+b );			5
        console.log ( typeof (a+b) );		number
        console.log ( a-b );			5
        console.log ( typeof (a-b) );		number
        console.log ( a-Boolean(b) );		5
        console.log ( typeof (a-Boolean(b)) );	number
        console.log ( Boolean(a)-b );		1
        console.log ( typeof (Boolean(a)-b) );	number
    
    
       console.log ( Boolean ( 0 ) );		flase
        console.log ( Boolean ( 0.0 ) );		flase
        console.log ( Boolean ( "0" ) );		true
        console.log ( Boolean ( null ) );		flase
        console.log ( Boolean ( "null" ) );		true
        console.log ( Boolean ( false ) );		flase
        console.log ( Boolean ( undefined ) );	flase
        console.log ( Boolean ( ' ' ) );		true
        console.log ( Boolean ( NaN ) );		flase
        console.log ( Boolean ( "false" ) );	true
        console.log ( Boolean ( "undefined" ) );   	true  
    
    隐式数据类型转换
    console.log ( 1 + "true" );		1true
    console.log ( 1 + undefined );  	NaN
    console.log ( 1 + null ); 		1
    console.log ( 1 + false );		1
    console.log ( 1 + NaN ); 		NaN
    console.log ( 1 + "null" ); 		1null
    console.log ( 1 + "10" ); 		110
    console.log ( "1" + 10 ); 		110
    console.log ( "1" + (10 + 1) );		111
    console.log ( "1" + 10 + 1 );``		1101
    console.log ( 10 + 1 + "1" );		111
    console.log ( !10 );			flase
    console.log ( !"10" );			flase
    console.log ( !"0" );			flase	
    console.log ( !0 );			true
    console.log ( !"null" );		flase	
    console.log ( !"" );			true
    console.log ( !"  " );			flase 
    

      

  • 相关阅读:
    leetcode 347. Top K Frequent Elements
    581. Shortest Unsorted Continuous Subarray
    leetcode 3. Longest Substring Without Repeating Characters
    leetcode 217. Contains Duplicate、219. Contains Duplicate II、220. Contains Duplicate、287. Find the Duplicate Number 、442. Find All Duplicates in an Array 、448. Find All Numbers Disappeared in an Array
    leetcode 461. Hamming Distance
    leetcode 19. Remove Nth Node From End of List
    leetcode 100. Same Tree、101. Symmetric Tree
    leetcode 171. Excel Sheet Column Number
    leetcode 242. Valid Anagram
    leetcode 326. Power of Three
  • 原文地址:https://www.cnblogs.com/zycs/p/12158396.html
Copyright © 2020-2023  润新知