How to check if variable is String type

coffeescript

Solution

use `typeof`

doSomething(result) if typeof result is 'string'

Note that `typeof` is an operator not a function so you don't write `typeof(result)`

You can also do this

doSomethingElse(result) if typeof result isnt 'string'

or even

return if typeof result is 'string'
   doSomething result
else
   doSomethingElse result

See http://coffeescript.org/#conditionals for more on `Coffeescript` conditionals.

Problem

I'm getting data with ajax, and the result can be either array of results or a string statement like "no results found". How can i tell whether i got any results or not? i tried this approach: ``` if result == String do something ``` but its not working, just like ``` if typeof(result) == "string" do something ``` Is there any other function that can help me get the type of the variable? Or maybe i can test it for Array type, it would also be very helpful

Original source