0
Apples & Oranges?Adventures in Equality Comparison & one of the ‘Bad Parts’               Kevin Munc - @muncman
Who needs transitivity?
Who needs transitivity?‘’ == ‘0’
Who needs transitivity?‘’ == ‘0’   false
Who needs transitivity?‘’ == ‘0’   false   okay...
Who needs transitivity?‘’ == ‘0’   false   okay...0 == ‘’
Who needs transitivity?‘’ == ‘0’   false   okay...0 == ‘’     true
Who needs transitivity?‘’ == ‘0’   false   okay...0 == ‘’     true    hmm...
Who needs transitivity?‘’ == ‘0’   false   okay...0 == ‘’     true    hmm...0 == ‘0’
Who needs transitivity?‘’ == ‘0’   false   okay...0 == ‘’     true    hmm...0 == ‘0’    true
Who needs transitivity?‘’ == ‘0’   false   okay...0 == ‘’     true    hmm...0 == ‘0’    true    uh...
The double-equals attempts      type coercion.
Crockford’s View=== and !== arethe equalitycomparisonoperators.== and != are the‘evil twins’.
Not Truthy? Falsy?
Not Truthy? Falsy?false == 0
Not Truthy? Falsy?false == 0      true
Not Truthy? Falsy?false == 0      truefalse == ‘0’
Not Truthy? Falsy?false == 0      truefalse == ‘0’    true
For crying out loud.
For crying out loud.10 == ’10’
For crying out loud.10 == ’10’       true
For crying out loud.10 == ’10’       true10 == ‘010’
For crying out loud.10 == ’10’       true10 == ‘010’      true
Madness
Madness10 == ’+10’
Madness10 == ’+10’   true
Madness10 == ’+10’      true‘ trn’ == 0
Madness10 == ’+10’      true‘ trn’ == 0   true
Triple is the new black       Triple Equals === Strict Equals When comparing objects, it checks identity. Otherwise, it ch...
Bonus Question
Given:NaN === NaN is false
Given:  NaN === NaN is falseHow can we make this true?
Given:    NaN === NaN is false How can we make this true?           NaN = 5     (it’s not a constant)isNaN() is preferred ...
Given:    NaN === NaN is false How can we make this true?           NaN = 5     (it’s not a constant)isNaN() is preferred ...
Questions? Comments?      The      BookPhoto Credshttp://covers.oreilly.com/images/9780596517748/lrg.jpghttp://www.flickr.c...
Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’
Upcoming SlideShare
Loading in...5
×

Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’

326

Published on

Method of the Month (MOTM) for Cbusjs (Columbus JavaScript Users Group) 2011.04

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
326
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • undefined != true // true\nnull != true // true\n\n
  • undefined != true // true\nnull != true // true\n\n
  • undefined != true // true\nnull != true // true\n\n
  • undefined != true // true\nnull != true // true\n\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • JavaScript has bad parts...\n...and good parts.\n
  • JavaScript has bad parts...\n...and good parts.\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Transcript of "Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’"

    1. 1. Apples & Oranges?Adventures in Equality Comparison & one of the ‘Bad Parts’ Kevin Munc - @muncman
    2. 2. Who needs transitivity?
    3. 3. Who needs transitivity?‘’ == ‘0’
    4. 4. Who needs transitivity?‘’ == ‘0’ false
    5. 5. Who needs transitivity?‘’ == ‘0’ false okay...
    6. 6. Who needs transitivity?‘’ == ‘0’ false okay...0 == ‘’
    7. 7. Who needs transitivity?‘’ == ‘0’ false okay...0 == ‘’ true
    8. 8. Who needs transitivity?‘’ == ‘0’ false okay...0 == ‘’ true hmm...
    9. 9. Who needs transitivity?‘’ == ‘0’ false okay...0 == ‘’ true hmm...0 == ‘0’
    10. 10. Who needs transitivity?‘’ == ‘0’ false okay...0 == ‘’ true hmm...0 == ‘0’ true
    11. 11. Who needs transitivity?‘’ == ‘0’ false okay...0 == ‘’ true hmm...0 == ‘0’ true uh...
    12. 12. The double-equals attempts type coercion.
    13. 13. Crockford’s View=== and !== arethe equalitycomparisonoperators.== and != are the‘evil twins’.
    14. 14. Not Truthy? Falsy?
    15. 15. Not Truthy? Falsy?false == 0
    16. 16. Not Truthy? Falsy?false == 0 true
    17. 17. Not Truthy? Falsy?false == 0 truefalse == ‘0’
    18. 18. Not Truthy? Falsy?false == 0 truefalse == ‘0’ true
    19. 19. For crying out loud.
    20. 20. For crying out loud.10 == ’10’
    21. 21. For crying out loud.10 == ’10’ true
    22. 22. For crying out loud.10 == ’10’ true10 == ‘010’
    23. 23. For crying out loud.10 == ’10’ true10 == ‘010’ true
    24. 24. Madness
    25. 25. Madness10 == ’+10’
    26. 26. Madness10 == ’+10’ true
    27. 27. Madness10 == ’+10’ true‘ trn’ == 0
    28. 28. Madness10 == ’+10’ true‘ trn’ == 0 true
    29. 29. Triple is the new black Triple Equals === Strict Equals When comparing objects, it checks identity. Otherwise, it checks value - without coercion. The implicit coercion of the ‘evil twins’ has a performance cost, as well. It’s better to make the coercion explicit if it is appropriate for your comparison.
    30. 30. Bonus Question
    31. 31. Given:NaN === NaN is false
    32. 32. Given: NaN === NaN is falseHow can we make this true?
    33. 33. Given: NaN === NaN is false How can we make this true? NaN = 5 (it’s not a constant)isNaN() is preferred for checks
    34. 34. Given: NaN === NaN is false How can we make this true? NaN = 5 (it’s not a constant)isNaN() is preferred for checksundefined can also be changed
    35. 35. Questions? Comments? The BookPhoto Credshttp://covers.oreilly.com/images/9780596517748/lrg.jpghttp://www.flickr.com/photos/thedepartment/131206032/http://www.flickr.com/photos/christinamatheson/108818379/http://www.flickr.com/photos/thebusybrain/2492945625/http://www.flickr.com/photos/kharied/3315558702/http://www.flickr.com/photos/44974843@N04/457397http://www.flickr.com/photos/teachernz/3222401608/
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×