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。

  • 相关阅读:
    [Android-NDK编译] ndk 编译 c++ 兼容性问题汇总整理
    [云计算] 001.云计算简介
    eatwhatApp开发实战(十一)
    eatwhatApp开发实战(十)
    [Unity2d系列教程] 002.引用外部DLL
    Istio Sidecar
    Kubernetes Dashborad 搭建
    Istio 1.6架构及性能
    kubeadm 搭建kubernetes集群环境
    docker 安装
  • 原文地址:https://www.cnblogs.com/reynold-lei/p/3726901.html
Copyright © 2011-2022 走看看