0

I have this function:

error = (error, modelState, modalHeader, modalBody): ng.IPromise<any> => {
    // function code here

In some cases the first parameter could be a string such as:

"problem" 

in other cases it could be an object such as this:

{
"ErrorMessage":"END_TEST - Invalid TestID, Unauthorized Access or TestStatus is not Started or Paused",
"ErrorNumber":50001
}

Is there a way that I could detect if it is a string or an object?

Alan2
  • 23,493
  • 79
  • 256
  • 450

1 Answers1

1

typeof operator is your friend:

error = (error, modelState, modalHeader, modalBody): ng.IPromise<any> => {
   if (typeof error === 'string') {
     //string
   } else if (typeof error === 'object') {
     //object
   }
}

typeof can evaluate to following: string, number, object, undefined, boolean, function.

Dmitri Pavlutin
  • 18,122
  • 8
  • 37
  • 41