JavaScript中的比较操作符>、<、>=、<=介绍_javascript技巧
程序员文章站
2022-04-03 16:56:24
...
与==操作符一样,比较操作符(>、=、
//In comparison, Date object is converted to number
var d = new Date();
var s1 = "Thu Mar 27 2008 14:57:11 GMT+0800 (CST)";
var s2 = "Thu Mar 27 2099 14:57:11 GMT+0800 (CST)";
var n1 = d.valueOf() - 1000;
var n2 = d.valueOf() + 1000;
console.log(d > s1);//false, d is converted to number, and that number is further converted to string. It is a string comparison here.
console.log(d > s2);//false
console.log(d > n1);//true
console.log(d > n2);//false
1.操作符两边如果有对象,将其转换成number;如果无法转换成number,则将其转换成string。
2.经过转换后,如果操作符两边均为string,则进行字符串比较;否则,只要有一边出现number,则进行数值比较。
3.如果操作符两边出现NaN,返回false。
4.0与-0相等。
实验
复制代码 代码如下:
//In comparison, Date object is converted to number
var d = new Date();
var s1 = "Thu Mar 27 2008 14:57:11 GMT+0800 (CST)";
var s2 = "Thu Mar 27 2099 14:57:11 GMT+0800 (CST)";
var n1 = d.valueOf() - 1000;
var n2 = d.valueOf() + 1000;
console.log(d > s1);//false, d is converted to number, and that number is further converted to string. It is a string comparison here.
console.log(d > s2);//false
console.log(d > n1);//true
console.log(d > n2);//false
console.log("11" > 3);//true
上一篇: 使用源生操作react-redux
下一篇: PHP相对于其它语言有哪些魅力?