How to know if an object is a string [closed]

1

When I double click on the cell of an ExtJs grid returns the uuid but when selecting the button I return the object as such.

How can I know if what returns to me is a string or an object?

    
asked by Bany Alvarado 15.03.2016 в 16:08
source

1 answer

4

In javascript the safest way to know if it is an object of type x you can do it with Object.prototype.toString since it will give you a result like [object Tipo] .

This solution may not work for the object because it can return [object Object] or something else depending on how they have implemented it.

If it will work for strings since it will always return [object String] so you can use the following function.

function isString(obj) {
    return Object.prototype.toString.call(obj) === '[object String]';
}

You can use typeof as well but I do not recommend it since

 console.log(typeof 'sdas');               => string
 console.log(typeof new String('sdas'));   => object
    
answered by 15.03.2016 / 16:30
source