4

The good:

'hello'.toString()    // "hello"
let obj = {}
obj.toString()        // "[object Object]"

The bad:

undefined.toString()  // throws TypeError
null.toString()       // throws TypeError

Are there any other types that will throw on the .toString() method?

halfer
  • 19,824
  • 17
  • 99
  • 186
danday74
  • 52,471
  • 49
  • 232
  • 283
  • 4
    `toString` is defined on `Object`, so any type other than undefined and null, which of course do not support any methods at all. –  Aug 15 '17 at 05:13
  • 1
    It's worth noting that this works with numeric literals as well, but due to the fact that the period serves double duty as decimal separator and member access operator, you do have to be aware of the syntax. `1.toString()` produces a syntax error, but `1..toString()` produces `"1"`. – p.s.w.g Aug 15 '17 at 05:20
  • [Not all of them do](https://stackoverflow.com/q/18640776/1048572). – Bergi Aug 15 '17 at 05:29

4 Answers4

4

From docs of toString()

Every object has a toString() method that is automatically called when the object is to be represented as a text value or when an object is referred to in a manner in which a string is expected. By default, the toString() method is inherited by every object descended from Object.

If the variable type is not object, it's going to throw.

So your best bet is you can check test instanceof Object before calling.

And it is worth mentioning that your code works with 1.8.5 version

var toString = Object.prototype.toString;
toString.call(undefined)  // gives [object Undefined]
toString.call(null)       // gives [object Null]

Note: Starting in JavaScript 1.8.5 toString() called on null returns [object Null], and undefined returns [object Undefined], as defined in the 5th Edition of ECMAScript and a subsequent Errata. See Using_toString()_to_detect_object_class.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • not only objects descended from Object has `toString` method. You can add this method manually to any object – Ivan Minakov Aug 15 '17 at 05:22
  • It's about the toString() derived from Object and yes you can create own methods on any object. – Suresh Atta Aug 15 '17 at 05:24
  • 1
    Calling methods like that on `undefined` and `null` will only throw exceptions, not yield strings. And no, this didn't change with JS1.8.5. – Bergi Aug 15 '17 at 05:30
  • @Bergi Would you please refer the linked docs ? I am quoting the docs. – Suresh Atta Aug 15 '17 at 05:32
  • @ꜱᴜʀᴇꜱʜᴀᴛᴛᴀ Would you mind trying it out? Yes, they do return those strings when called on `null` and `undefined`, but what you have shown is not how to do that. "See *[Using toString() to detect object class*](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/toString#Using_toString()_to_detect_object_class)." – Bergi Aug 15 '17 at 05:34
  • @Bergi the way I did now even won't work with before 1.8.5. But works with 1.8.5 – Suresh Atta Aug 15 '17 at 05:49
3

every obj that inherited from Object has to string method.

Starting from javascript Starting in JavaScript 1.8.5 toString() called on null returns [object Null], and undefined returns [objectUndefined].

You can read more about it at this link: You can read more about it at this link

matisa
  • 497
  • 3
  • 25
1

Calling toString() on objects without this method in prototype chain will cause an error. For example:

let a = Object.create(null); 
a.toString() //TypeError
Fabien
  • 4,862
  • 2
  • 19
  • 33
Ivan Minakov
  • 1,432
  • 7
  • 12
1

toString is defined on Object, so any type other than undefined and null will inherit toString.

sabrehagen
  • 1,517
  • 1
  • 13
  • 38