zoukankan      html  css  js  c++  java
  • JS中的“==”符号及布尔值转换规则

    what are the rules for how == converts types?

    关于"=="的比较规则:

    1. Comparing numbers and strings will always convert the strings to numbers. 

    number类型与string类型比较,string会转换为number类型。如:2=="2" true

    2. null and undefined will always equal each other.

    null类型与undefined类型比较始终相等。如:var a = null,b; 则 a==b 为true。

    3. Comparing booleans to any other type will always cause the booleans to be converted to numbers.

    布尔类型与其他任何类型进行比较,布尔类型将会转换为number类型。如:var a = 0, b = false;则a==b为true

    4. Comparing numbers or strings to objects will always cause the numbers or strings to be converted to objects.

    number类型或者string类型与object类型进行比较,number或者string类型都会转换为object类型。如:var a = 0, b = {};则a==b为false

    The rules for converting other types to booleans are actually relatively straightforward:

    关于其他类型转换为布尔类型的规则:

    1. undefined and null are always false.

    2. Booleans are just treated as booleans (obviously).

    3. Numbers are false if they equal 0 or NaN; otherwise, they’re true.

    4. Strings are true, except for the empty string "", which is false.

    5. Objects are always true.

  • 相关阅读:
    洛谷 P2958 [USACO09OCT]木瓜的丛林Papaya Jungle
    洛谷 P1400 塔
    10-2 集合之List
    主从数据库
    【单元测试】
    Pen Editor
    appendGrid
    动画
    JavaScript框架设计 第14章 动画引擎
    >>>
  • 原文地址:https://www.cnblogs.com/ywxgod/p/3969316.html
Copyright © 2011-2022 走看看