instanceof String not behaving as expected in Google Apps Script

Viewed 3782

I wanted to check whether a variable in an Apps Script was a String, but found that instanceof wasn't returning true when the variable was in fact a string. The following test:

function test_instanceof() {
  var a = "a";
  Logger.log('"a" is ' + ((a instanceof String) ? '' : 'not ') + 'a String');
  var b = String("b");
  Logger.log('String("b") is ' + ((b instanceof String) ? '' : 'not ') + 'a String');
}

Logs these two messages:

"a" is not a String
String("b") is not a String

The docs aren't clear on the subset of ECMAScript that is supported, though apparently instanceof is a valid operator and String is a valid type, judging from the fact that the code executed without an exception.

What is the appropriate way to check the type of a variable?

1 Answers
Related