SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: JS syntax ? :

  1. #1
    SitePoint Zealot
    Join Date
    Jul 2002
    Location
    Fort Lauderdale
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JS syntax ? :

    Can someone write a line or two using ? and : in js so that I can understand what these do. I've seen them used in code, but don't understand and its hard to search yahoo for ? :

    www.unpossiblemedia.com - A Fort Lauderdale based multimedia company.

  2. #2
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Well I'm a PHP programmer but I guess it would be the same

    Code:
    var foo = 5;
    var bar;
     
    bar = ( foo == 5 ) ? true : false;
    What that does is this. If foo is equal to five (5), then bar will be true else if foo doesn't equal five (5) then bar will be false.

    Another example -
    Code:
    var foo = true;
     
    ( foo ) ? doSomething() : doSomethingElse();
    In that example, if foo is true, then the function doSomething() will be executed but if foo is false, then the function doSomethingElse() will be executed.

    If you don't understand after that post back and I'll try to explain it another way.

  3. #3
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also here is a link to a tutorial on the subject:
    http://www.javascriptkit.com/javatutors/varshort.shtml

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is oftentimes called a ternary statement or ternary operator. I don't know which is more accurate, but it is oftentimes just shortened to ternary anyways.

    Basically, a quick one-line way to do if...else statements assuming you have only one command for each condition.

    Note: they can also be "nested" to create if...else if....else like statments

    var weight = 100;
    var category = ( weight < 50 ) ? 'light' : ( weight < 120 ) ? 'medium' : 'heavy';

    Of course, as with all code 'shortcuts', you sacrifice readability for brevity
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    SitePoint Zealot
    Join Date
    Jul 2002
    Location
    Fort Lauderdale
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My inclination was that it was some type of cryptic if statement. Thanks for clearing it up.

    www.unpossiblemedia.com - A Fort Lauderdale based multimedia company.


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
  •