数值转换题

Posted zycs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数值转换题相关的知识,希望对你有一定的参考价值。

一,  请写出下列表达式的值
      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 

  

以上是关于数值转换题的主要内容,如果未能解决你的问题,请参考以下文章

数值转换题

前端面试题总结

字符串中数字与整型数字的转化

剑指Offer-面试案例面试题66:把字符串转为整数

万变不离核心基础第四关——表示二,八,十,十六进制的数值/四大进制之间的转换

在代码片段中包含类型转换