TIL coding
  • Initial page
  • 排版
  • Flexbox
  • Grid
  • jQuery
  • Untitled
  • JavaScript
    • An Introduction to JavaScript
    • Hello, world!
    • Code structure
    • The modern mode, "use strict"
    • Variables
    • Data types
    • Type Conversions
    • Operators
    • Comparisons
    • Interaction: alert, prompt, confirm
    • Conditional operators: if, '?'
    • Logical operators
    • Loops: while and for
    • The "switch" statement
    • Functions
    • Function expressions and arrows
    • JavaScript specials
    • Comments
    • Ninja code
    • Automated testing with mocha
    • Polyfills
    • Objects
    • Garbage collection
    • Symbol type
    • Object methods, "this"
    • Object to primitive conversion
    • Constructor, operator "new"
    • Methods of primitives
    • Numbers
    • Strings
    • Arrays
    • Array methods
    • Iterables
    • Map, Set, WeakMap and WeakSet
    • Object.keys, values, entries
    • Destructuring assignment
    • Date and time
    • JSON methods, toJSON
    • Recursion and stack
    • Rest parameters and spread operator
    • Closure
    • The old "var"
    • Global object
    • Function object, NFE
    • The "new Function" syntax
    • Scheduling: setTimeout and setInterval
    • Decorators and forwarding, call/apply
    • Function binding
    • Currying and partials
    • Arrow functions revisited
    • Property flags and descriptors
    • Property getters and setters
    • Prototypal inheritance
    • F.prototype
    • Native prototypes
    • Prototype methods, objects without __proto__
    • The “class” syntax
    • Class inheritance
    • Static properties and methods
    • Private and protected properties and methods
    • Extending built-in classes
    • Class checking: "instanceof"
    • Mixins
    • Error handling, "try..catch"
    • Custom errors, extending Error
    • Introduction: callbacks
    • Promise
    • Promises chaining
    • Error handling with promises
    • Promise API
  • Bootstrap
    • Navbar
Powered by GitBook
On this page
  • Boolean is the result
  • String comparison
  • Comparison of different types
  • Strict equality
  • Comparison with null and undefined

Was this helpful?

  1. JavaScript

Comparisons

>、<、>=、<=、==、!=、===、!==

Boolean is the result

如同其他運算符號,比較運算符號返回一個值,這值為布林值。

alert( 2 > 1 );  // true (correct)
alert( 2 == 1 ); // false (wrong)
let result = 5 > 4; // assign the result of the comparison
alert( result ); // true

String comparison

比較 2 個字串的規則:用 Unicode 由左到右比較字母大小,大小寫式有分別,小寫比較大。

Comparison of different types

比較不同類型,會把值轉換成數字。

alert( '2' > 1 ); // true, string '2' becomes a number 2
alert( '01' == 1 ); // true, string '01' becomes a number 1

Strict equality

==、!= 有問題,會把類型轉換。

alert( 0 == false ); // true

===、!== 不會有類型轉換。

alert( 0 === false ); // false, because the types are different

Comparison with null and undefined

alert( null == undefined ); // true
alert( null === undefined ); // false

alert( null > 0 );  // false ∵ null = 0
alert( null == 0 ); // false ∵ null == undefined
alert( null >= 0 ); // true ∵ null = 0

alert( undefined > 0 ); // false ∵ undefined = NaN
alert( undefined < 0 ); // false ∵ undefined = NaN
alert( undefined == 0 ); // false ∵ null == undefined

不需要記得這麼多,如果值可能出現null或undefined,只要使用===,就可以避免複雜情況發生。

PreviousOperatorsNextInteraction: alert, prompt, confirm

Last updated 5 years ago

Was this helpful?