Nodejs Number Type Check isNumber()

Here you can find the source of isNumber()

Method Source Code

/**// w w  w  . j  a  v a2  s  .  co m
 * Created on 1/14/2015.
 */
var assert=require('assert');
var Number=function(number){
    this.number = number;
}
Number.prototype.isNumber=function(){
    return isNaN(this.number);
}
Number.prototype.isEven=function(){
    return this.number%2==0;
}
Number.prototype.isOdd=function(){
    return this.number%2!=0;
}

var evenNumber=new Number(12);
console.log("Is number even? "+evenNumber.isEven());

var oddNumber=new Number(13);
console.log("Is number odd? "+isNaN(oddNumber));

var strangeNumber=new Number("strange");

//verification through unit test
assert.equal(evenNumber.isEven(), true);
assert.equal(oddNumber.isOdd(), true);
assert.equal(strangeNumber.isNumber(), true);

Related

  1. isInt()
    Number.prototype.isInt = function() {
      return parseFloat(this) == parseInt(this, 10) && !isNaN(this);
    };
    
  2. isInteger()
    Number.prototype.isInteger = function () {
        "use strict";
        var ans = false;
        if (this % 1 == 0) {
            ans = true;
        return ans;
    };
    
  3. isInteger()
    Number.prototype.isInteger = function(){
      var ans = false;
      if(this%1==0){
        ans = true;
      return ans;
    };
    
  4. isInteger()
    Number.prototype.isInteger = function(){
      var ans = false;
      if(this % 1 == 0){
        ans = true
      return ans;
    
  5. isNumber()
    Number.prototype.isNumber = function()
      try
        var value = this.toString();
        value = value.replace(",","");
          if (parseFloat(value)!= value)
            return false;
          else
    ...
    
  6. isNumber(input)
    function isNumber(input) {
        return typeof input === "number" ? true : false;
    
  7. isNumber(n)
    function isNumber(n) {
       return typeof n == 'number';
    
  8. isNumber(n)
    function isNumber(n) 
      return !isNaN(parseFloat(n)) && isFinite(n);