Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Useless attempt to make JS == acceptable

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

Useless attempt to make JS == acceptable

Lightweight talk about how we can accept JavaScript's double equals operator in our heart. LOL no.

Avatar for Hendra G.

Hendra G.

June 30, 2014

Other Decks in Programming

Transcript

  1. Java’s equality 1 == 1 1 != “1” Object a

    = new Object(); Object b = new Object(); // false a == b Compare the reference!
  2. Ruby’s equality # all true 1 == 1 ‘a’ ==

    ‘a’ (2-1) == 1 1 != ‘1’ [] == [] {} == {} {} != {:a => 1}
  3. Javascript’s equality // looks fine true == true false ==

    false 5 == 5 0 != 1 ‘foo’ == ‘foo’
  4. Javascript’s quiz [[]] != ‘’ [[]] != ‘0’ [[]] ==

    [] [4] != 4 [7,7] == ‘7, 7’ [1,2] == ‘1,2’ [[1,2],3] == ‘[1,2],3’ [0,-0] == ‘0,-0’ false true false false false true false false
  5. ???

  6. NaN is not NaN > typeof(6) ‘number’ > typeof(NaN) ‘number’

    > NaN == NaN false > NaN === NaN false it also won’t be equal to other types
  7. object too > foo = {a:1, b:2} > bar =

    {a:1, b:2} > foo == {a:1, b:2} false > foo == bar false > {a:1, b:2} == {a:1, b:2} false
  8. when possible, parseInt(string) ‘1’ == 1 ‘-10’ == -10 ‘8a’

    != 8 21 == ‘2’ + ‘1’ ‘32’ == 16 * 2 -1 != ‘1’ ‘’ == 0
  9. true ⇔ 1 false ⇔ 0 TRUE only equals to

    number 1 > true == 1 > true != 5 > true == Infinity FALSE only equals to number 0 > false == 0 > false != -5 > false != -Infinity
  10. Array#toString() > [1] == ‘1’ // so, [1] == 1

    > [‘1’] == ‘1’ // so, [‘1’] == 1 > [] == ‘’ > [] == 0 > [] != [] > [[]].toString() // ‘’ > [].toString() // ‘’
  11. but, use Array#valueOf() for array > [1].valueOf() // [ 1

    ] > [‘1’].valueOf() // [ ‘1’ ] > [].valueOf() // [] > [[]].valueOf() // [ [] ] // not deep equal > [0] != [0] > [] != [] > [‘3’] != [‘3’]
  12. null == undefined typeof(undefined) // ‘undefined’ value for declared but

    has not yet be assigned a value typeof(null) // ‘object’ null is considered a placeholder for an object - representing no value
  13. null == undefined “This was actually an error in the

    original JavaScript implementation that was then copied in ECMAScript” - Professional JavaScript for Web Developers
  14. the rest are straightforward unparseable string only equals with the

    same ‘abc’ == ‘abc’ Infinity is simply constant of a number Infinity == Infinity Infinity == (-Infinity * -1)
  15. NaN (NaN Ain’t NaN) not deep equal object-object & array-array

    parseInt string when possible use toString() to array when meet non-array null == undefined (and use well-known rules / common sense) ;)