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 5 of 5
  1. #1
    New Coder
    Join Date
    Sep 2010
    Posts
    46
    Thanks
    1
    Thanked 1 Time in 1 Post

    Javascript code meaning

    I am having trouble with the meaning of this piece of Javascript code:

    Code:
    x=((next - present) == 356) ? 2 : (((present - last) == 382) ? 1 : 0);
    Could someone explain it for me.

  • #2
    New Coder
    Join Date
    Jun 2009
    Posts
    81
    Thanks
    0
    Thanked 8 Times in 8 Posts
    x=((next - present) == 356) ? 2 : (((present - last) == 382) ? 1 : 0);


    next present and last are to hold numeric values

    the JavaScript ternary operator is used to assign a value to x
    x = (comparison_that_resolves_to_true_or_false)? value_returned_if_true : value_returned_if_false;

    In your code provided x is assigned the value 2 if next is greater than present by exactly 356.
    If next is any other value, x is assigned the value 1 if present is greater than last by exactly 382.
    If present is any other value, x is assigned the value of 0.

    in other words,

    x is assigned the value 2 if next-present equals 356.
    if next-present does not equal 356 then
    x is assigned the value 1 if present-last equals 382.
    if present-last does not equal 382 then
    x is assigned the value 0.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    A longer version of the same code that does exactly the same thing using if statements instead of ternary operators is:

    Code:
    if ((next - present) == 356) x = 2;
    else if ((present - last) == 382) x = 1;
    else x=  0;
    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.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,159
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    Quote Originally Posted by felgall View Post
    A longer version of the same code that does exactly the same thing using if statements instead of ternary operators is:

    Code:
    if ((next - present) == 356) x = 2;
    else if ((present - last) == 382) x = 1;
    else x=  0;
    Longer, but much clearer. Doubt if there is any discernable speed difference.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Philip M View Post
    Doubt if there is any discernable speed difference.
    In terms of execution time I would guess that if you ran a few billion iterations that the speed difference might just about become large enough to measure in milliseconds.

    I haven't actually counted the number of ternary references in jQuery but their use there possibly saves Gigabytes a month of bandwidth on the servers delivering copies of the library to the hundreds of millions of people visiting pages that use that library - making it well worth the trouble of using them as much as possible in place of if statements for that library. Individuals viewing the pages still wouldn't notice any significant difference in load times though.
    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
    •