Arithmetic Operations

Program demonstrating all arithmetic operations

BeginnerTopic: Basic Programs
Back

JavaScript Arithmetic Operations Program

This program helps you to learn the fundamental structure and syntax of JavaScript programming.

Try This Code
let a = 10;
let b = 3;

// Addition
let sum = a + b;
console.log(`${a} + ${b} = ${sum}`);

// Subtraction
let difference = a - b;
console.log(`${a} - ${b} = ${difference}`);

// Multiplication
let product = a * b;
console.log(`${a} * ${b} = ${product}`);

// Division
let quotient = a / b;
console.log(`${a} / ${b} = ${quotient}`);

// Modulus (Remainder)
let remainder = a % b;
console.log(`${a} % ${b} = ${remainder}`);

// Exponentiation (ES6)
let power = a ** b;
console.log(`${a} ** ${b} = ${power}`);

// Increment
let x = 5;
x++; // Post-increment
console.log("After x++:", x);

let y = 5;
++y; // Pre-increment
console.log("After ++y:", y);

// Decrement
let p = 5;
p--; // Post-decrement
console.log("After p--:", p);

let q = 5;
--q; // Pre-decrement
console.log("After --q:", q);
Output
10 + 3 = 13
10 - 3 = 7
10 * 3 = 30
10 / 3 = 3.3333333333333335
10 % 3 = 1
10 ** 3 = 1000
After x++: 6
After ++y: 6
After p--: 4
After --q: 4

Understanding Arithmetic Operations

This program demonstrates all arithmetic operators in JavaScript.

Basic Arithmetic Operators

1.

Addition (+)

: Adds two numbers

   5 + 3; // 8
   

2.

Subtraction (-)

: Subtracts second from first

   5 - 3; // 2
   

3.

Multiplication (*)

: Multiplies two numbers

   5 * 3; // 15
   

4.

Division (/)

: Divides first by second

   10 / 3; // 3.333...
   

5.

Modulus (%)

: Returns remainder after division

   10 % 3; // 1
   10 % 2; // 0 (even number)
   

6.

Exponentiation (

)**: Raises to power (ES6)
   2 ** 3; // 8 (2³)
   

Increment/Decrement Operators

Post-increment (`x++`):

Returns value first, then increments
let x = 5;
let y = x++; // y = 5, x = 6

Pre-increment (`++x`):

Increments first, then returns value
let x = 5;
let y = ++x; // y = 6, x = 6

Order of Operations (PEMDAS)

1.Parentheses
2.Exponentiation
3.Multiplication/Division (left to right)
4.Addition/Subtraction (left to right)
2 + 3 * 4;     // 14
(2 + 3) * 4;   // 20
2 ** 3 + 1;    // 9

Special Cases

10 / 0;        // Infinity
-10 / 0;       // -Infinity
0 / 0;         // NaN (Not a Number)
10 % 0;        // NaN

Type Coercion

JavaScript converts types automatically:

"5" + 3;       // "53" (string concatenation)
"5" - 3;       // 2 (number subtraction)
"5" * 3;       // 15 (number multiplication)

Let us now understand every line and the components of the above program.

Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.

Practical Learning Notes for Arithmetic Operations

This JavaScript program is part of the "Basic Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents