Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Does JavaScript follow BOMDAS?

Options
  • 21-05-2013 9:23pm
    #1
    Closed Accounts Posts: 79 ✭✭


    Hi all,

    Studying for an exam and was wondering if JavaScript uses the BOMDAS rule?

    So how would this calculate?

    21 + 3 % 6 * 3 + 5?

    What would be the answer to that and how did you come to that answer?


Comments

  • Registered Users Posts: 5,246 ✭✭✭conor.hogan.2


    Chrome, safari, firefox and IE (and Opera and others probably) come with a javascript console, so you could run "21 + 3 % 6 * 3 + 5" quicker than you could post this thread.


  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 2,588 Mod ✭✭✭✭KonFusion


    35


  • Closed Accounts Posts: 79 ✭✭pheno


    Thanks all.

    How does this work parseInt("0xF");? or parseInt("A", 16);?


  • Registered Users Posts: 101 ✭✭Ethan.Saaris


    The answer to your first question is yes, Javascript follows the BODMAS rule by default. Try this in an HTML page:
    <script>
    // default Javascript BODMAS
    document.write(21 + 3 % 6 * 3 + 5); // result is 35
    document.write('<br>');
    
    // separated percentage
    document.write(21 + (3 % 6) * 3 + 5); // result is 35
    document.write('<br>');
    
    // separated percentage and multiplication
    document.write(21 + ((3 % 6) * 3) + 5); // result is 35
    document.write('<br>');
    </script>
    

    The answer to your second question is that both are correct:

    "If the radix parameter is omitted, JavaScript assumes the following:

    If the string begins with "0x", the radix is 16 (hexadecimal)
    If the string begins with "0", the radix is 8 (octal). This feature is deprecated
    If the string begins with any other value, the radix is 10 (decimal)"


Advertisement