SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ternary operator vs && and ||

    Is there any difference between
    Code:
    var answer = test ? value1 : value2;
    and
    Code:
    var answer = test && value1 || value 2;
    when value1 is not falsey?

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The second example is 1 byte longer, always performs 2 comparisons, and requires that value1 be truthy.

    Aside from that, they are identical.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •