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!