How to use Divide Operator in Javascript

Description

The divide operator is represented by a slash /.


var result = 6 / 2;
console.log(result);

If either of the operands isn't a number, it is converted to a number behind the scenes using the Number() casting function. This means that an empty string is treated as 0, and the Boolean value of true is treated as 1.

The divide operator has special behaviors for special values.

  • If the operands are numbers, regular arithmetic division is performed.
  • If the result is out of range, it returns either Infinity or -Infinity.
  • If either operand is NaN, the result is NaN.
  • If Infinity is divided by Infinity, the result is NaN.
  • If zero is divided by zero, the result is NaN.
  • If a nonzero finite number is divided by zero, the result is either Infinity or -Infinity, depending on the sign of the first operand.
  • If Infinity is divided by any number, the result is either Infinity or -Infinity, depending on the sign of the second operand.
  • If either operand isn't a number, it is converted to a number using Number().

Example


var result = 6 / 2;
console.log(result);

The code above generates the following result.





















Home »
  Javascript »
    Javascript Introduction »




Script Element
Syntax
Data Type
Operator
Statement
Array
Primitive Wrapper Types
Function
Object-Oriented
Date
DOM
JSON
Regular Expressions