Sunday, August 3, 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

Handling Null, Undefined and Zero Values in JavaScript

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


In JS, it’s easy to get caught out when determining if a variable is null, undefined, or has a value of zero. I do a lot of data visualisation, and quite often I’ll need to filter out null values from an array of data.

data.filter((value) => !!value)

The problem is, this is also going to filter out values of 0 too, because is tests whether the value is falsy. (Other falsy values include empty strings, and NaN.)

A better way might for this particular case might be to filter out non-numerical values instead:

data.filter((value) => typeof value === \’number\’)

Logical operators can be handy for providing default values. Using the || (or) operator, the message text will read “Unknown days since last post” if the daysSinceLastPost value doesn’t exist.

const message = `${daysSinceLastPost || \’Unknown\’} days since last post`

The issue here is that if daysSinceLastPost is 0, the text will read “Unknown days since last post” too! Instead, we could use the ?? operator, otherwise known as the nullish coalescing operator.

const message = `${daysSinceLastPost ?? \’Unknown\’} days since last post`

That way, daysSinceLastPost will be used, even if the value is 0, while our fallback will be used if it is null or undefined.

Put even more simply: In the following example, test1 will evaluate to \’Test 1\’, while test2 will evaluate to0. Nice!

const test1 = 0 || \’Test 1\’

const test2 = 0 ?? \’Test 2\’

console.log(test1) // \’Test 1\’

console.log(test2) // 0



Source link

Tags: cssfront endHandlingJavascriptNullUndefinedValuesweb designweb development
Previous Post

Webinar: Motion control solutions for robotics

Next Post

Why Are So Many People Moving Into Green Homes?

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
Why Are So Many People Moving Into Green Homes?

Why Are So Many People Moving Into Green Homes?

Top 6 Programming Languages to Learn in 2023 | Tanay Pratap Hindi

Top 6 Programming Languages to Learn in 2023 | Tanay Pratap Hindi

Israel War Coverage Live | Israel cuts off food, water and power to Gaza: Day 3 update | Hamas

Israel War Coverage Live | Israel cuts off food, water and power to Gaza: Day 3 update | Hamas

Leave a Reply Cancel reply

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

  • Trending
  • Comments
  • Latest
Amazon’s Bedrock and Titan Generative AI Services Enter General Availability

Amazon’s Bedrock and Titan Generative AI Services Enter General Availability

October 2, 2023
9 Best Open Source Text-to-Speech (TTS) Engines

9 Best Open Source Text-to-Speech (TTS) Engines

April 9, 2024
Fireworks AI Open Sources FireLLaVA: A Commercially-Usable Version of the LLaVA Model Leveraging Only OSS Models for Data Generation and Training

Fireworks AI Open Sources FireLLaVA: A Commercially-Usable Version of the LLaVA Model Leveraging Only OSS Models for Data Generation and Training

January 24, 2024
Link between adversity, psychiatric and cognitive decline

Link between adversity, psychiatric and cognitive decline

March 1, 2024
Creating Fluid Typography with the CSS clamp() Function — SitePoint

Creating Fluid Typography with the CSS clamp() Function — SitePoint

April 22, 2024
Digital Marketing Trends 2022 | Digital Marketing Future And Career In 2022 | Simplilearn

Digital Marketing Trends 2022 | Digital Marketing Future And Career In 2022 | Simplilearn

October 15, 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