Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Oct 2005
    Posts
    196
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Advantage of the ternary operator instead of the if statement

    Like the topic says, is there a time when you should use the ternary operator instead of the if statement? I've Googled a bunch, but can't find a good answer to my question.

    Thank you.

  • #2
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    Basically use it if you are assigning a string to a variable based on if something is true or false.

  • #3
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Personally, I don't like them that much (I find if's more readable), but I do occasionally use them. It's more about personal preference than best practice.

  • #4
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,169
    Thanks
    19
    Thanked 65 Times in 64 Posts
    I use them whenever possible, once you understand them they're far more readble than if {}else{}. I dont know about performance wise but they're much easier and neater to type

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Yeah KC, all you have to do is understand them and you'll like em better! haha.

    I don't like them either; if's are more readable to me. Just a personal preference is all.

  • #6
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,169
    Thanks
    19
    Thanked 65 Times in 64 Posts
    Quote Originally Posted by Fumigator
    Yeah KC, all you have to do is understand them and you'll like em better! haha.

    I don't like them either; if's are more readable to me. Just a personal preference is all.
    They just take getting used to. Your fingers will love you for it

  • #7
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm offering no rational reason for disliking them; I just do. They're too...crowded for my tastes.

    I do tend to use them when pulling values out of a POST form, so I can do some basic validation at the same time.

  • #8
    Regular Coder
    Join Date
    Oct 2005
    Posts
    196
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for all of your replies.

    I'll just stick with what I was using them for!

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,454
    Thanks
    0
    Thanked 632 Times in 622 Posts
    The two ways of coding are equivalent. Which you use would be a matter of personal preference. The ternary operator version is shorter and so is faster to type and would be about 0.0005 milliseconds faster to process due to being faster to read in (since it is shorter).
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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