每天一篇javascript学习小结(基础知识)

var s1 = "01"; var s2 = "1.1"; var s3 = "z";//字母'z'无法转换为数字,所以或返回NaN var b = false; var f = 1.1; var o = { valueOf: function() { return -1; } }; s1 = -s1; //value becomes numeric -1 s2 = -s2; //value becomes numeric -1.1 s3 = -s3; //value becomes NaN b = -b; //value becomes numeric 0 f = -f; //change to -1.1 o = -o; // 1 先执行对象的valueOf()方法返回-1,--1为1     o = +o;//-1 o的valueOf()值为-1,+-1还是-1

2、特殊字符运算

var result1 = 5 - true; //4 because true is converted to 1 var result2 = NaN - 1; //NaN NaN不是一个数字,和任何数字做任何运算都是NaN var result3 = 5 - 3; //2 var result4 = 5 - ""; //5 because "" is converted to 0 var result5 = 5 - "2"; //3 because "2" is converted to 2 var result6 = 5 - null; //5 because null is converted to 0

3、变量转字符串运算

var value1 = 10; var value2 = true; var value3 = null; var value4;//value4 没有赋值就是underfined 转换为字符串就是'underfined' alert(String(value1)); //"10" alert(String(value2)); //"true" alert(String(value3)); //"null" alert(String(value4)); //"undefined"

4、数字的进制转换 

var num = 10; alert(num.toString()); //"10"默认十进制 alert(num.toString(2)); //"1010"二进制 alert(num.toString(8)); //"12"八进制 alert(num.toString(10)); //"10"十进制 alert(num.toString(16)); //"a"十六进制

5、字符串比较运算

var result1 = 5 > 3; //true var result2 = 5 < 3; //false var result3 = "Brick" < "alphabet"; //true 字符串比较按照字母表顺序进行比较 小写字母在大写字母后面 var result4 = "Brick".toLowerCase() < "alphabet".toLowerCase(); //false字母表顺序比较 var result5 = "23" < "3"; //true 字符2小于字符3 var result6 = "23" < 3; //false 此时'23'转换成23了 var result7 = "a" < 3; //false because "a" becomes NaN 字符'a'无法转换成数字 var result8 = NaN < 3; //false NaN 和任何数字比较,都无法转换成数字,所以一直是false var result9 = NaN >= 3; //false

6、字符进制转换

var num1 = parseInt("AF", 16); //175 按照16进制输出十进制数据 10*16+15*1 var num2 = parseInt("AF"); //NaN 没有指定进制,默认按照10进制转换,由于AF不在十进制范围,返回NaN alert(num1); alert(num2);

7、parseInt的使用

var num1 = parseInt("1234blue"); //1234 var num2 = parseInt(""); //NaN 字符''无法转换成数字 var num3 = parseInt("0xA"); //10 - hexadecimal 16进制的A var num4 = parseInt(22.5); //22 var num5 = parseInt("70"); //70 - decimal var num6 = parseInt("0xf"); //15 16进制为15

8、Number对象的使用

var num1 = Number("Hello world!"); //NaN var num2 = Number(""); //0 空字符串可以转换成0 这个parseInt()不一样 var num3 = Number("000011"); //11 var num4 = Number(true); //1

9、NaN用法     

alert(NaN == NaN); //false alert(isNaN(NaN)); //true alert(isNaN(10)); //false ?10 is a number alert(isNaN("10")); //false ?can be converted to number 10 alert(isNaN("blue")); //true ?cannot be converted to a number alert(isNaN(true)); //false ?can be converted to number 1

10、系统最大数字

var result = Number.MAX_VALUE + 1; alert(isFinite(result)); // false

11、Infinity无穷大

alert(5 * 6); //30 alert(5 * NaN); //NaN alert(Infinity * 0); //NaN alert(Infinity * 2); //Infinity alert("5" * 5); //25 alert(true * 10); //10 alert(false * 10); //0       alert(26 % 5); //1       alert(Infinity % 3); //NaN       alert(3 % 0); //NaN       alert(5 % Infinity); //5       alert(0 % 10); //0       alert(true % 25); //1       alert(3 % false); //NaN

12、for in 循环

for (var propName in window) { document.write(propName); document.write("<br />"); }

13、特殊字符比较   

alert(null == undefined); //true alert(null === undefined); //false alert("NaN" == NaN); //false alert("NaN" === NaN); //false alert(NaN == NaN); //false alert(NaN === NaN); //false alert(NaN != NaN); //true alert(NaN !== NaN); //true alert(false == 0); //true alert(false === 0); //false alert(true == 1); //true alert(true === 1); //false alert(null == 0); //false alert(undefined == 0); //false alert(5 == "5"); //true alert(5 === "5"); //false

14、Boolean对象  

var message = "Hello world!"; var messageAsBoolean = Boolean(message); alert(messageAsBoolean); //true

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgzfss.html