Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Does JavaScript follow BOMDAS?

  • 21-05-2013 09: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

  • Posts: 0 ✭✭✭ [Deleted User]


    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.


  • Registered Users, Registered Users 2 Posts: 2,559 ✭✭✭KonFusion


    35


  • Closed Accounts Posts: 79 ✭✭pheno


    Thanks all.

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


  • Registered Users, Registered Users 2 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