---
layout: page
title: "JavaScript is_finite function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/is_finite:441
- /functions/view/is_finite
- /functions/view/441
- /functions/is_finite:441
- /functions/441
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's is_finite

{% codeblock math/is_finite.js lang:js https://raw.github.com/kvz/phpjs/master/functions/math/is_finite.js raw on github %}
function is_finite (val) {
  // From: http://phpjs.org/functions
  // +   original by: Onno Marsman
  // *     example 1: is_finite(Infinity);
  // *     returns 1: false
  // *     example 2: is_finite(-Infinity);
  // *     returns 2: false
  // *     example 3: is_finite(0);
  // *     returns 3: true
  var warningType = '';

  if (val === Infinity || val === -Infinity) {
    return false;
  }

  //Some warnings for maximum PHP compatibility
  if (typeof val === 'object') {
    warningType = (Object.prototype.toString.call(val) === '[object Array]' ? 'array' : 'object');
  } else if (typeof val === 'string' && !val.match(/^[\+\-]?\d/)) {
    //simulate PHP's behaviour: '-9a' doesn't give a warning, but 'a9' does.
    warningType = 'string';
  }
  if (warningType) {
    throw new Error('Warning: is_finite() expects parameter 1 to be double, ' + warningType + ' given');
  }

  return true;
}
{% endcodeblock %}

 - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/math/is_finite.js)

Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are 
the closest match to this hashtable-like data structure. 

Please also note that php.js offers community built functions and goes by the 
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online 
functions that are far from perfect, in the hopes to spark better contributions. 
Do you have one? Then please just: 

 - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/math/is_finite.js)

### Example 1
This code
{% codeblock lang:js example %}
is_finite(Infinity);
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
false
{% endcodeblock %}

### Example 2
This code
{% codeblock lang:js example %}
is_finite(-Infinity);
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
false
{% endcodeblock %}

### Example 3
This code
{% codeblock lang:js example %}
is_finite(0);
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
true
{% endcodeblock %}


### Other PHP functions in the math extension
{% render_partial _includes/custom/math.html %}
