Division by zero javascript

Javascript division by 0

In JavaScript, division by zero does not cause an any error. While, it generates Infinity, which is a reserved word in JavaScript.

Let’s try to create simple example:

Example 1 : divided by zero;

  • In this code, we will divide the positive integers by 0.
  1. < script type = "text/javascript" >
  2. var a = 10 , b = 0 ;
  3. var division ;
  4. division = a / b ;
  5. console . log ( division );
  6. < /script >
Output :

Example 2: divided by zero;

  • In this code, we will divide the negative integers by 0.
  1. < script type = "text/javascript" >
  2. var a = -10 , b = 0 ;
  3. var division ;
  4. division = a / b ;
  5. console . log ( division );
  6. < /script >
Output :
Recommended Posts:
  • Get link
  • Facebook
  • Twitter
  • Pinterest
  • Email
  • Other Apps

Labels

  • Get link
  • Facebook
  • Twitter
  • Pinterest
  • Email
  • Other Apps

Comments

java program for student details using inheritance

# Java program to print student details using «single-level» inheritance In this section, You will learn how to print student details using single inheritance in java, with Scanner and without Scanner class. 1.) With » Scanner » class Let’s try to create a simple example : ➤ Example : student.java; import java.util.Scanner; class StudentDetails < int roll_no ; String name , cl ; //creating a function to take student details void input () < Scanner sc = new Scanner ( System . in ); System . out . print ( "Ente

  • Get link
  • Facebook
  • Twitter
  • Pinterest
  • Email
  • Other Apps

Django static files not working when debug false || debug true

# Django static and media files not working when debug is false In this article you will learn how to fix problem of not loading static files and media in Django even the DEBUG is FALSE. This is the easiest and safest solution. # Problem: Django static and media files not working when debug is false ➤ Code: settings.py DEBUG = False #True ALLOWED_HOSTS = [ ‘*’ ] #Host name # Problem Fix: Let’s see, How you can fix the problem of Django static and media files not working when DEBUB = False : 1.)First way: devserver in insecure mode If you still need to server static locally ( e.g. for testing without debug ) you can run devserver in insecure mode: python manage.py runserver —insecure —insecure: it means you can run serve

  • Get link
  • Facebook
  • Twitter
  • Pinterest
  • Email
  • Other Apps
Читайте также:  Поиск работы программист python

Java program for student mark list using class and object — java code examples with output

Program to print student mark list using class and object In this section, You will learn how to calculate and print student total marks lists of n nuumber of student in java, with the help of InputStreamReader and BufferedReader class. java program to calculate and display the total and the n student marks Let’s try to create a simple example : ➤ Example : student.java; import java.io.*; class MList < static public StudentsInfo theStudents = new StudentsInfo () ; public static void ViewRecords () < System . out . println ( "_______________________________________________________________&

  • Get link
  • Facebook
  • Twitter
  • Pinterest
  • Email
  • Other Apps

Источник

Division (/)

The division ( / ) operator produces the quotient of its operands where the left operand is the dividend and the right operand is the divisor.

Try it

Syntax

Description

The / operator is overloaded for two types of operands: number and BigInt. It first coerces both operands to numeric values and tests the types of them. It performs BigInt division if both operands becomes BigInts; otherwise, it performs number division. A TypeError is thrown if one operand becomes a BigInt but the other becomes a number.

For BigInt division, the result is the quotient of the two operands truncated towards zero, and the remainder is discarded. A RangeError is thrown if the divisor y is 0n . This is because number division by zero returns Infinity or -Infinity , but BigInt has no concept of infinity.

Examples

Basic division

1 / 2; // 0.5 Math.floor(3 / 2); // 1 1.0 / 2.0; // 0.5 1n / 2n; // 0n 5n / 3n; // 1n -1n / 3n; // 0n 1n / -3n; // 0n 2n / 2; // TypeError: Cannot mix BigInt and other types, use explicit conversions // To do division with a BigInt and a non-BigInt, convert either operand 2n / BigInt(2); // 1n Number(2n) / 2; // 1 

Division by zero

2.0 / 0; // Infinity 2.0 / 0.0; // Infinity, because 0.0 === 0 2.0 / -0.0; // -Infinity 2n / 0n; // RangeError: Division by zero 

Specifications

Browser compatibility

BCD tables only load in the browser

Читайте также:  nth-child

See also

Found a content problem with this page?

This page was last modified on Mar 28, 2023 by MDN contributors.

Your blueprint for a better internet.

MDN

Support

Our communities

Developers

Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.

Источник

RangeError: BigInt division by zero

The JavaScript exception «BigInt division by zero» occurs when a BigInt is divided by 0n .

Message

RangeError: Division by zero (V8-based) RangeError: BigInt division by zero (Firefox) RangeError: 0 is an invalid divisor value. (Safari)

Error type

What went wrong?

The divisor of a division or remainder operator is 0n . In Number arithmetic, this produces Infinity , but there’s no «infinity value» in BigInts, so an error is issued. Check if the divisor is 0n before doing the division.

Examples

Division by 0n

const a = 1n; const b = 0n; const quotient = a / b; // RangeError: BigInt division by zero 

Instead, check if the divisor is 0n first, and either issue an error with a better message, or fallback to a different value, like Infinity or undefined .

const a = 1n; const b = 0n; const quotient = b === 0n ? undefined : a / b; 

See also

Found a content problem with this page?

This page was last modified on Jul 24, 2023 by MDN contributors.

Your blueprint for a better internet.

Источник

How does JavaScript handle Divide by Zero?

Many candidates are rejected or down-leveled in technical interviews due to poor performance in behavioral or cultural fit interviews. Ace your interviews with this free course, where you will practice confidently tackling behavioral interview questions.

Читайте также:  Extract numbers in string java

Overview

Divide by Zero is considered a special case by most programming languages. Any number can never be divided by zero because its result is indeterminate. This shot covers how JavaScript handles a Divide by Zero expression.

How do programming languages handle it?

Most programming languages throw an exception at run time. For handling the exception, they use a try-catch block.

How does JavaScript handle it?

JavaScript acts differently from other programming languages. When it encounters any Divide by Zero expression, it doesn’t throw an exception.

Now, the question may arise of why JavaScript behaves differently and why it doesn’t throw any exception.

The possible reasons for this are as follows:

  1. JavaScript is a dynamically typed language, and it performs type-coercion .
  2. Throwing exceptions at runtime is inconvenient for JavaScript.

So, how does JavaScript evaluate these expressions?

Let’s see that in the following example.

Example

// Declare and initialize a number with 0
const num1 = 0;
// Declare and initialize a number with a positive integer
const num2 = 10;
// Declare and initialize a number with a negative integer
const num3 = -10;
// Divide by zero with a zero
console.log("Divide by zero with a zero:", num1 / 0);
// Divide by zero with a positive integer
console.log("Divide by zero with a positive integer:", num2 / 0);
// Divide by zero with a negative integer
console.log("Divide by zero with a negative integer:", num3 / 0);

Explanation

  • Line 2: We declare and initialize a variable num1 with 0.
  • Line 5: We declare and initialize a variable num2 with a positive value.
  • Line 8: We declare and initialize a variable num3 with a negative value.
  • Line 11: We divide the variable num1 with 0, and print the output on the console.
  • Line 14: We divide the variable num2 with 0, and print the output on the console.
  • Line 17: We divide the variable num3 with 0, and print the output on the console.

Output

The output of the code in JavaScript is as follows:

  • Dividing the number 0 by 0 returns NaN.
  • Dividing the positive number by 0 returns Infinity.
  • Dividing the negative number by 0 returns -Infinity.

Источник

Оцените статью