Does Javascript Follow BODMAS

Does Javascript follow BODMAS?

Yes, Javascript follows the order of operation popularly known as BODMAS. I wrote a very simple program in Javascript to try this out, and prove it. You can follow along in the demonstration below.

We will try to evaluate the following to see if the results match.

3^2 + 9 x 7 – 12 ÷ 4

9 x (24-4)÷5+3^2

Let’s start with the first one: According to BODMAS (Bracket, Order, Division, Multiplication, Addition, Subtraction), we will start with 3 to the power 2, followed by 12 divided by 4, then 9 multiplied by 7, and finish off with additions and subtraction.

3^2 = 9, we have 9+9×7-12÷4

12÷4 = 3, we 9+9×7-3

9×7 = 63, 9+63-3

finally, 9+63-3 = 69

So, using BODMAS, we get 69 as the answer to 3^2 + 9 x 7 – 12 ÷ 4

Now, let’s do it using a simple Javascript program, note that Math.pow(3,2) is 3^2

<script>
        Answer1 = Math.pow(3,2)+9*7-12/4;
        console.log('Answer is:', Answer1);
</script>

When you run the program and check your console, the result will be 69 as shown in the screenshot below.

JS code and results

Let’s consider the second one with brackets: 9 x (24-4)÷5+3^2

by following BODMAS, we will start with (24-4), followed by 3 to power 2, and so on.

(24-4) = 20, we have 9×20÷5+3^2

3^2 = 9, we have 9×20÷5+9

20÷5 = 4, we have 9×4+9

9×4 = 36, we 36+9

finally, we have 36+9 = 45 which is the answer to 9 x (24-4)÷5+3^2 according to BODMAS.

We can do this one as well with a simple Javascript program shown below

<script>
        Answer2 = 9*(24-4)/5 + Math.pow(3,2);
        console.log('Answer is:', Answer2);
</script>

When you run this JS program in the browser, the answer will be 45 as shown in the screenshot below.

JS code and results 2

I tried out a lot of evaluations using Javascript and BODMAS, the answer was always the same. It can be concluded that Javascript follows BODMAS. You can try it as well.