javascript : Object.isNumber

 

javascript : Object.isNumber


Target:

Applicable with any javascript Object/String Variable that holds numeric value .


Description :

When an object holds an numeric value then it return true otherwise undefined. basically it is defined in Number prototype. Many time in programming we face a problem that an object variable holds Numeric value or other object structure then it help us. Before writing the code of Numeric calculation we can check whether a variable holds Numeric value or not.


Note: It is an property of Numeric, that is defined in Numeric Prototype.
It is useful to check an object variable is Numeric or any other type.

another way to check variable holds numeric value is "typeof(variable) = 'number'"
but when variable holds string numeric value it return "typeof(variable) = 'string'"



Syntax:

Object.isNumber

return true when object hold's Numeric value otherwise undefined


Usase:

  1. var obj = 55;
    console.log('typeof obj = ' + typeof(obj));
    if(obj.isNumber){
         console.log('Object holds Numeric value');
    }
    else{
         console.log('Object is not a number');
    }
    It return
    "typeof obj = number"
    "Object holds Numeric value".
  2. var obj = 55.55;
    console.log('typeof obj = ' + typeof(obj));
    if(obj.isNumber){
         console.log('Object holds Numeric value');
    }
    else{
         console.log('Object is not a number');
    }
    It return
    "typeof obj = number"
    "Object holds Numeric value".
  3. var obj = '55';
    console.log('typeof obj = ' + typeof(obj));
    if(obj.isNumber){
         console.log('Object holds Numeric value');
    }
    else{
         console.log('Object is not a number');
    }
    It return
    "typeof obj = string"
    "Object holds Numeric value".
  4. var obj = '55.05';
    console.log('typeof obj = ' + typeof(obj));
    if(obj.isNumber){
         console.log('Object holds Numeric value');
    }
    else{
         console.log('Object is not a number');
    }
    It return
    "typeof obj = string"
    "Object holds Numeric value".
  5. var obj = [55];
    console.log('typeof obj = ' + typeof(obj));
    if(obj.isNumber){
         console.log('Object holds Numeric value');
    }
    else{
         console.log('Object is not a number');
    }
    It return
    "typeof obj = object"
    "Object is not a number".
  6. var obj = '55px';
    console.log('typeof obj = ' + typeof(obj));
    if(obj.isNumber){
         console.log('Object holds Numeric value');
    }
    else{
         console.log('Object is not a number');
    }
    It return
    "typeof obj = string"
    "Object is not a number".
  7. var obj = '55.55.5';
    console.log('typeof obj = ' + typeof(obj));
    if(obj.isNumber){
         console.log('Object holds Numeric value');
    }
    else{
         console.log('Object is not a number');
    }
    It return
    "typeof obj = string"
    "Object is not a number".


Download MYJS.js

MyJs is a collection of java script extension methods that enhances the JavaScript objects, class object, DOM element, domElementsCollections, Date, String, Number and other data type. 

Include in you project landing page (master page) and it will extends your DOM and JavaScript Objects.

Comments

Popular posts from this blog

javascript : String.right

javascript : String.left

javascript : .nextAll()