Number Is Negative Zero

Beginner

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore how to determine whether a given number is a negative zero or not, using JavaScript. We will use a simple function that checks if the value is equal to zero and if 1 divided by the value equals -Infinity. This lab will help you understand the concept of negative zero in JavaScript and how to detect it in your code.

Checking for Negative Zero

To check if a number is negative zero, open the Terminal/SSH and enter node. Then, use the following code:

const isNegativeZero = (val) => val === 0 && 1 / val === -Infinity;

This will check if the passed value is equal to 0 and if 1 divided by the value equals -Infinity. For example:

isNegativeZero(-0); // true
isNegativeZero(0); // false

Summary

Congratulations! You have completed the Number Is Negative Zero lab. You can practice more labs in LabEx to improve your skills.