var arr = [1,2];
var arr1 = arr ;
arr.push(3);
document.write(arr1)
答案:arr1 = [1,2,3]
( 理解stack heap原理)
var arr = [1,2];
var arr1 = arr;
arr = [1,2,3]
document.write(arr1)
答案:arr1 = [1,2]
var a = "10" > "8";
document.write(a)
答案:false
字符串比较原则 第一位先进行比较,再进行第二位比较..
var a = 1 && 2+2;
document.write(a);
答案:4
var a = 1 + 1 && 1 - 1;
document.write(0);
答案:0
打印100以内的所有质数
var count = 0;
for (i = 1; i < 100; i++) {
for (j = 1; j <= i; j++) {
if (i % j == 0) {
count++;
}
}
if (count == 2) {
console.log(i + ' ')
}
count = 0;
}
写出斐波那契数列
var n = parseInt(window.prompt('input'));
var first = 1;
var second = 1;
var third;
if (n > 2) {
for (i = 0; i < n - 2; i++) {
third = first + second;
first = second;
second = third;
}
document.write(third);
}else {
document.write(1);
}
var num = null;
console.log(typeof(num));
答案:object
null在最早作为空对象使用(历史遗留问题)
typeof能返回六种数据类型:
number,string,object,undefined,function,boolean
var num = 1 + '1';
console.log(typeof (num) + ':' + num);
答案:string:11
JS独有的隐式类型转换
var num = 1 * '1';
console.log(typeof (num) + ':' + num);
答案:number:1
var num = 1 - '1';
console.log(typeof (num) + ':' + num);
答案:number:0
var a = undefined;
var num = Number(a);
console.log(num);
答案:NaN
var num = true;
console.log(typeof (parseInt(num)) + ' ' + parseInt(num))
答案:Number : NaN
Number()更关注值,parseInt()更关注转换类型。
需要注意的是parseInt不是四舍五入,而是直接去掉小数。
var num = parseInt(10,16);
console.log(num);
答案:16
parseInt(num,radix)是以16进制为基底,将num转化为以10进制的数字
var demo = '100px';
console.log(parseInt(demo));
答案:100
parseInt读到非数字类为止
var demo = undefined;
var num = demo.toString();
console.log(typeof (num) + ':' + num);
答案:报错
undefined与null 不能使用toString()
var demo = 10;
var num = demo.toString(8);
console.log(typeof (num) + ':' + num);
答案:string:12
radix为8 ,以8进制为基底
var demo = undefined;
var num = String(demo);
console.log(typeof (num) + ':' + num);
答案:string:undefined
String()方法,写什么就转化什么为字符串
console.log(isNaN("123"))
答案:false
console.log(isNaN("abc"))
答案:true
如果对于隐式类型转换不太了解可以看看:
JavaScript隐式类型转换
网友评论