According to the information found in the documentation about the toString() method:
The toString() method is automatically triggered when an object needs to be represented as a text value or when it is referenced in a context where a string is expected. This method is inherited by all objects that are descendants of Object.
If the variable type is not an object, an error will occur.
To avoid this, you can use the code snippet test instanceof Object
before making the call.
It's important to note that this code is designed to work with version 1.8.5.
var toString = Object.prototype.toString;
toString.call(undefined) // returns [object Undefined]
toString.call(null) // returns [object Null]
In JavaScript 1.8.5 and later versions, calling toString() on null returns [object Null], while calling it on undefined returns [object Undefined], as specified in the 5th Edition of ECMAScript and subsequent Errata. For more details, refer to Using_toString()_to_detect_object_class.