Friday, May 9, 2025
News PouroverAI
Visit PourOver.AI
No Result
View All Result
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing
News PouroverAI
No Result
View All Result

NaN or Not a Number?

November 9, 2023
in Front-Tech
Reading Time: 2 mins read
0 0
A A
0
Share on FacebookShare on Twitter



Following yesterday’s post on handling null, undefined, and zero values in JS, I was asked on Mastodon: I’m curious why isNaN() doesn’t work in your case. Thanks!

isNan() is a global function, which determines if a value evaluates to NaN or not. But what is NaN?

NaN (not to be confused with your grandma) in JavaScript stands for Not A Number. It’s relatively common for NaN to appear in JS code as a result of an impossible math operation, which can occur when passing a value of the wrong type into a function.

For example:
“`html
console.log(1 / undefined) // NaN
console.log(1 / ‘abc’) // NaN
“`

NaN is a mathematical expression (rather than something that comes from JavaScript), and so, confusingly can also be thought of as a number! We can see that it is actually a type of number:
“`html
const value = 1 / ‘abc’
console.log(value) // NaN
console.log(typeof value) // ‘number’
“`

We can use isNaN() to test whether a value evaluates to NaN. But it can be surprising. ‘1’, ‘abc’, ” (an empty string), null, and ‘002’ are all clearly not numbers. But if we pass them into isNaN(), only two of them evaluate true:
“`html
console.log(isNaN(‘1’)) // false
console.log(isNaN(”)) // false
console.log(isNaN(‘abc’)) // true
console.log(isNaN(null)) // false
console.log(isNaN(undefined)) // true
console.log(isNaN(‘002’)) // false
“`

isNan() attempts first to convert the value to a number. null or an empty string are coerced to 0, for instance, meaning they can be parsed as numbers. Of these examples, only ‘abc’ and undefined cannot be parsed as numbers.

On the other hand, we can use the more robust Number.isNaN(), which does not first attempt to convert the value to a number:
“`html
console.log(Number.isNaN(‘1’)) // false
console.log(Number.isNaN(”)) // false
console.log(Number.isNaN(‘abc’)) // false
console.log(Number.isNaN(null)) // false
console.log(Number.isNaN(undefined)) // false
console.log(Number.isNaN(‘002’)) // false
“`

In this case, all of the expressions evaluate false.

Filtering data

Going back to yesterday’s example of filtering an array to remove null or undefined values, we can see that isNaN() (or Number.isNaN()) won’t cut it.

“`html
const data = [100, 52, null, 12, 71, ”, undefined]
data.filter((value) => !isNaN(value))
// Results in [100, 52, null, 12, 71, ”]

data.filter((value) => !Number.isNaN(value))
// Results in [100, 52, null, 12, 71, ”, undefined]
“`

These filter functions don’t determine whether a value is a number, only whether a value is not Not A Number (NaN)! Therefore, non-number values won’t all be filtered out.

To truly ensure that our filtered data only includes numbers, we can check if the type of each value is ‘number’:
“`html
data.filter((value) => typeof value === ‘number’)
“`

However, as we’ve seen above, NaN is a type of number! So NaN values won’t be filtered out here.
“`html
const value = 1 / ‘abc’
const data = [100, 52, null, 12, 71, ”, undefined, value]
data.filter((value) => typeof value === ‘number’)
// Results in: [100, 52, 12, 71, NaN]
“`

To make our function even more robust, let’s use isNaN() to cover those too:
“`html
data.filter((value) => { return typeof value === ‘number’ && !isNaN(value)})
// Results in: [100, 52, 12, 71] ☺️
“`



Source link

Tags: cssfront endNaNNumberweb designweb development
Previous Post

Dealing With a Clogged Drain? Try These 5 Solutions

Next Post

TDI 37 – Ryan Swanstrom

Related Posts

The essential principles of a good homepage
Front-Tech

The essential principles of a good homepage

June 7, 2024
How to measure and improve user retention
Front-Tech

How to measure and improve user retention

June 6, 2024
Push Animation on Grid Items
Front-Tech

Push Animation on Grid Items

June 5, 2024
How to build a Rails API with rate limiting
Front-Tech

How to build a Rails API with rate limiting

June 4, 2024
Introduction to the B.I.A.S. framework
Front-Tech

Introduction to the B.I.A.S. framework

June 3, 2024
Blue Ridge Ruby is exactly what we need
Front-Tech

Blue Ridge Ruby is exactly what we need

June 3, 2024
Next Post
TDI 37 – Ryan Swanstrom

TDI 37 – Ryan Swanstrom

I Analysed 2,000 Cloud Jobs Ads: Here’s What You NEED To Know

I Analysed 2,000 Cloud Jobs Ads: Here's What You NEED To Know

Fixing Canada’s grocery prices problem: the Competition Bureau’s plan | About That

Fixing Canada's grocery prices problem: the Competition Bureau's plan | About That

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Trending
  • Comments
  • Latest
Is C.AI Down? Here Is What To Do Now

Is C.AI Down? Here Is What To Do Now

January 10, 2024
Porfo: Revolutionizing the Crypto Wallet Landscape

Porfo: Revolutionizing the Crypto Wallet Landscape

October 9, 2023
A Complete Guide to BERT with Code | by Bradney Smith | May, 2024

A Complete Guide to BERT with Code | by Bradney Smith | May, 2024

May 19, 2024
A faster, better way to prevent an AI chatbot from giving toxic responses | MIT News

A faster, better way to prevent an AI chatbot from giving toxic responses | MIT News

April 10, 2024
Part 1: ABAP RESTful Application Programming Model (RAP) – Introduction

Part 1: ABAP RESTful Application Programming Model (RAP) – Introduction

November 20, 2023
Saginaw HMI Enclosures and Suspension Arm Systems from AutomationDirect – Library.Automationdirect.com

Saginaw HMI Enclosures and Suspension Arm Systems from AutomationDirect – Library.Automationdirect.com

December 6, 2023
Can You Guess What Percentage Of Their Wealth The Rich Keep In Cash?

Can You Guess What Percentage Of Their Wealth The Rich Keep In Cash?

June 10, 2024
AI Compared: Which Assistant Is the Best?

AI Compared: Which Assistant Is the Best?

June 10, 2024
How insurance companies can use synthetic data to fight bias

How insurance companies can use synthetic data to fight bias

June 10, 2024
5 SLA metrics you should be monitoring

5 SLA metrics you should be monitoring

June 10, 2024
From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

June 10, 2024
UGRO Capital: Targeting to hit milestone of Rs 20,000 cr loan book in 8-10 quarters: Shachindra Nath

UGRO Capital: Targeting to hit milestone of Rs 20,000 cr loan book in 8-10 quarters: Shachindra Nath

June 10, 2024
Facebook Twitter LinkedIn Pinterest RSS
News PouroverAI

The latest news and updates about the AI Technology and Latest Tech Updates around the world... PouroverAI keeps you in the loop.

CATEGORIES

  • AI Technology
  • Automation
  • Blockchain
  • Business
  • Cloud & Programming
  • Data Science & ML
  • Digital Marketing
  • Front-Tech
  • Uncategorized

SITEMAP

  • Disclaimer
  • Privacy Policy
  • DMCA
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2023 PouroverAI News.
PouroverAI News

No Result
View All Result
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing

Copyright © 2023 PouroverAI News.
PouroverAI News

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In