JS中连续比较问题

在js中使用连续 <>时:

  if (1 < 3 < 2) {
    console.log('111');
  } else {
    console.log('222');
  } // 111

1<3为true,true和2比较时,true转化为1,所以结果为true,打印了111;同理,false转化为0

例:

  if (1 > 5 < 2) {
    console.log('111');
  } else {
    console.log('222');
  } // 111

Number与Boolean比较,都会将两边的值转化为Number

在遇到连续多个值的逻辑比较时,建议使用:&&||

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。