www.webdeveloper.com
Results 1 to 5 of 5

Thread: unknown syntax

  1. #1
    Join Date
    Apr 2005
    Location
    Vancouver, BC
    Posts
    129

    unknown syntax

    I keep seeing this syntax, but I don't know what's it's called or what it does:
    Code:
    thing == 0 ? "a" : "b" + thing
    can someone please explain this?

    thanks

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    1,007
    That is considered a conditional operator in javascript. Essentially you have 3 parts
    thing == 0 ? "a" : "b" + thing

    The first part is a condition or comparison. If this condition is true then the second part is the value that is returned. If that condition is false then the third and final part is returned instead. So if 'thing' is equal to 0 then that statement would return "a", otherwise it will return "b" + thing.

  3. #3
    Join Date
    Apr 2005
    Location
    Vancouver, BC
    Posts
    129
    thanks

  4. #4
    Join Date
    Sep 2008
    Location
    Dallas
    Posts
    154
    thing == 0 ? "a" : "b" + thing

    is shorthand for

    if (thing == 0) {
    return "a";
    } else {
    return "b" + thing;
    }

  5. #5
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    ^ Except you don't return from a function when using ?: . A better example might be

    a = b ? c : d;

    which would be shorthand for

    if (b) {
    a = c;
    } else {
    a = d;
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles