Number Is Negative Zero

JavaScriptJavaScriptBeginner
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") subgraph Lab Skills javascript/variables -.-> lab-28426{{"`Number Is Negative Zero`"}} javascript/data_types -.-> lab-28426{{"`Number Is Negative Zero`"}} javascript/arith_ops -.-> lab-28426{{"`Number Is Negative Zero`"}} javascript/comp_ops -.-> lab-28426{{"`Number Is Negative Zero`"}} end

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.

Other JavaScript Tutorials you may like