Free JavaScript course. Sign Up for tracking progress →

JavaScript: Commutative operations

Do you remember the basic rule of arithmetic that "changing the order of the numbers we are adding doesn't change the sum"? It's called the commutative law.

A binary operation is commutative since swapping operands gets you the same result. Obviously, addition is a commutative operation: 3 + 2 = 2 + 3.

But is subtraction a commutative operation? Of course not: 2 - 3 ≠ 3 - 2. In programming, this law applies just like it does in arithmetic.

Moreover, most of the operations we face in real life are not commutative. Here is the conclusion: always pay attention to the order of things you work with.

Instructions

Write a program that adds and prints the values of "3 to the power of 5" and "-8 divided by -4".

Definitions

  • Commutativity is a property of an operation wherein changing the order of the operands does not change the result. For example, addition is a commutative operation: changing the order of the numbers we are adding does not change the sum.