News for hackers. Stuff that matters.
Re:Type Checking in JavaScript by nailer on Wednesday November 06, 2013 @09:08AM (#6682704)
I use 'kind' from Agave for this. It uses the same technique (Object.prototype.toString() plus edge case handling) but ships with lots of units tests to prove it:

https://agavejs.org

    ### Numbers

        kind(37) === 'Number'
        kind(3.14) === 'Number'
        kind(Math.LN2) === 'Number'
        kind(Infinity) === 'Number'
        kind(Number(1)) === 'Number'
        kind(new Number(1)) === 'Number'

    ### NaN

        kind(NaN) === 'NaN'

    ### Strings

        kind('') === 'String'
        kind('bla') === 'String'
        kind(String("abc")) === 'String'
        kind(new String("abc")) === 'String'


    ### Booleans

        kind(true) === 'Boolean'
        kind(false) === 'Boolean'
        kind(new Boolean(true)) === 'Boolean'

    ### Arrays

        kind([1, 2, 4]) === 'Array'
        kind(new Array(1, 2, 3)) === 'Array'

    ### Objects

        kind({a:1}) === 'Object'
        kind(new Object()) === 'Object'

    ### Dates

        kind(new Date()) === 'Date'

    ### Functions

        kind(function(){}) === 'Function'
        kind(new Function("console.log(arguments)")) === 'Function'
        kind(Math.sin) === 'Function'

    ### undefined

        kind(undefined) === 'undefined'

    ### null

        kind(null) === 'null'

There are no comments on this comment yet.