phpjs
Version:
85 lines (70 loc) • 2.32 kB
Markdown
layout: page
title: "JavaScript is_nan function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/is_nan:447
- /functions/view/is_nan
- /functions/view/447
- /functions/is_nan:447
- /functions/447
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's is_nan
{% codeblock math/is_nan.js lang:js https://raw.github.com/kvz/phpjs/master/functions/math/is_nan.js raw on github %}
function is_nan (val) {
// From: http://phpjs.org/functions
// + original by: Onno Marsman
// + input by: Robin
// * example 1: is_nan(NaN);
// * returns 1: true
// * example 2: is_nan(0);
// * returns 2: false
var warningType = '';
if (typeof val === 'number' && isNaN(val)) {
return true;
}
//Some errors 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_nan() expects parameter 1 to be double, ' + warningType + ' given');
}
return false;
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/math/is_nan.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_nan.js)
### Example 1
This code
{% codeblock lang:js example %}
is_nan(NaN);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
true
{% endcodeblock %}
### Example 2
This code
{% codeblock lang:js example %}
is_nan(0);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
false
{% endcodeblock %}
### Other PHP functions in the math extension
{% render_partial _includes/custom/math.html %}