Course contentsShow
← JavaScript
Lesson 241 of 2,7237. Arithmetic and OperatorsFree lesson

Division by Zero and Infinity

Understand JavaScript's treatment of division by zero, producing Infinity or -Infinity rather than errors.

Division by Zero and Infinity

What You'll Learn

You'll discover that JavaScript handles division by zero differently than many languages—producing special Infinity values instead of crashing your program.

The Unique Behavior

In many programming languages, dividing by zero causes an error that stops your code. JavaScript takes a different approach: it returns a special numeric value called Infinity (or -Infinity for negative results).

When you divide a positive number by zero, you get Infinity:

let result = 5 / 0;
console.log(result); // Infinity

When you divide a negative number by zero, you get -Infinity:

let result = -10 / 0;
console.log(result); // -Infinity

Why This Matters

Think of infinity like the horizon—you can point toward it, but you can't actually reach it. JavaScript acknowledges that dividing by zero represents an operation heading toward an infinitely large value, so it gives you a special marker rather than throwing an error.

This means your code keeps running, but you need to be aware that Infinity behaves differently in calculations:

console.log(Infinity + 1); // Still Infinity
console.log(Infinity * 2); // Still Infinity
console.log(10 / Infinity); // 0

Watch Out for Zero Divided by Zero

There's one special case: dividing zero by zero produces NaN (Not a Number), since this operation is mathematically undefined:

let unclear = 0 / 0;
console.log(unclear); // NaN

Key Takeaway: JavaScript's division by zero produces Infinity or -Infinity (not errors), allowing your code to continue running—but you should check for these special values when they could cause logic problems.