zoukankan      html  css  js  c++  java
  • javascript 比较

    javascript中由于是弱类型,所以在比较的时候有较大的麻烦。这次专门做了总结:

    Comparison Operators

    Comparison operators are used in logical statements to determine equality or difference between variables or values.

    Given that x=5, the table below explains the comparison operators:

    Operator

    Description

    Comparing

    Returns

    ==

    equal to

    x == 8

    false

    x == 5

    true

    ===

    exactly equal to (equal value and equal type)

    x === "5"

    false

    x === 5

    true

    !=

     not equal

    x != 8

    true

    !==

     not equal (different value or different type)

    x !== "5"

    true

    x !== 5

    false

     greater than

    x > 8

    false

     less than

    x < 8

    true

    >=

     greater than or equal to

    x >= 8

    false

    <=

     less than or equal to

    x <= 8

    true

    1. === vs ==

    JavaScript has both strict and type-converting equality comparison. For strict equality the objects being compared must have the same type and:

    • Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
    • Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another.
    • Two Boolean operands are strictly equal if both are true or both are false.
    • Two objects are strictly equal if they refer to the same Object.
    • Null and Undefined types are == (but not ===). [I.e. Null==Undefined (but not Null===Undefined)]

    2. > & <

    Checking that strings are integers is separate to comparing if one is greater or lesser than another. You should always compare number with number and string with string as the algorithm for dealing with mixed types not easy to remember.

    '00100'<'1'// true

    as they are both strings so only the first zero of '00100' is compared to '1' and because it's charCode is lower, it evaluates as lower.

    However:

    '00100'<1// false

    as the RHS is a number, the LHS is converted to number before the comparision.

    不过 最好在比较前 做类型转换: parseInt   Number  parseFloat。

  • 相关阅读:
    OSGI .Net 框架学习
    ArcEngine开发过程中的空间关系
    ITOCControl添加鼠标右键菜单
    IHookHelper的使用
    ITopologicalOperator接口调用
    GeoProcessor的使用方法
    ArcEngine内置工具条
    OSGI.NET插件方式开发你的应用
    C#在linux上运行实现
    Linux 下随机启动自己的应用 -请使用while(true) 不要Console.ReadKey()
  • 原文地址:https://www.cnblogs.com/reynold-lei/p/3726901.html
Copyright © 2011-2022 走看看