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。

  • 相关阅读:
    poj4474 Scout YYF I(概率dp+矩阵快速幂)
    网络编程之TCP异步群聊:服务器端代码
    平衡树(AVL)详解
    网络编程之TCP异步群聊:客户端代码
    [置顶] android 图片库的封装
    oracle的nvl函数的使用解析
    七天美音英标学习总结
    软考(7)——看图心想 标准化和知识产权
    Node.js学习(7)----包
    Ubuntu bitnami gitlab 安装
  • 原文地址:https://www.cnblogs.com/reynold-lei/p/3726901.html
Copyright © 2011-2022 走看看