The isFinite() is a function that simply checks if the value passed in it is finite number or not. It’s basically used to filter out NaN, undefined or Inifinite numbers. It used to make your code more error-free and robust.
Let’s see some examples to get a better idea.
const Age;
function getAge(a){
if(a){
this.Age = a;
}
}
getAge(undefined) //THIS WILL STORE 'undefined' IN VARIABLE 'Age'
Rather, DO THIS:
const Age;
function getAge(a){
if(isFinite(a)){ //THIS WILL BE FALSE IN CASE OF NaN OR undefined
this.Age = a;
}
else{
this.Age = 0;
}
}
getAge(undefined) //THIS WILL STORE 0 IN VARIABLE 'Age'
Or if you want to make it cleaner and 1 liner, you could use Ternary operator like so:
const Age;
function getAge(a){
this.Age = isFinite(a) ? a : 0;
}
getAge(undefined) //THIS WILL STORE 0 IN VARIABLE 'Age'
Want to learn what Ternary Operators are, check out here!
You could also take it a step further and make it more robust by checking if it’s of Number datatype like so:
const Age;
function getAge(a){
if(Number.isFinite(a)){ //THIS WILL BE FALSE IN CASE OF NaN OR undefined
this.Age = a;
}
else{
this.Age = 0;
}
}
getAge(undefined) //THIS WILL STORE 0 IN VARIABLE 'Age'
This way, it will also robustly check for the datatype to be only Number. Before it would have still accepted any number in string quotations i.e. “5”. Now it will make the condition as false if it’s in quotation and would only be true if it’s only a Number datatype.
That’s it!
Recent Posts
Recent Comments
Archives
- October 2025
- August 2025
- June 2025
- October 2024
- September 2024
- August 2024
- June 2023
- May 2023
- March 2023
- December 2022
- November 2022
- October 2022
- September 2022
- August 2022
- July 2022
- June 2022
- May 2022
- April 2022
- February 2022
- January 2022
- December 2021
- November 2021
- October 2021
- September 2021
- August 2021
- July 2021
Categories
- Angular
- AWS
- Backend Development
- Big Data
- Casino
- Cloud
- Database
- Deployment
- DevOps
- Docker
- Frontend Development
- GitHub
- Google Cloud Platform
- Installations
- Java
- JavaScript
- Linux
- MySQL
- Networking
- NodeJS
- Operating System
- Python
- Python Flask
- Report
- Security
- Server
- SpringBoot
- Subdomain
- TypeScript
- Uncategorized
- VSCode
- Webhosting
- WordPress
Search
Recent Post
Lopebet’s Multi-Bet Options: How and When to
- 23 October, 2025
- 2 min read
Understanding Bonuses and Promotions Offered by Tez888
- 30 August, 2025
- 2 min read
Hvordan norske mobilcasino støtter veldedige formål og
- 5 June, 2025
- 2 min read