# JS Tip of the Day: Symbol.toStringTag

**URL:** <https://forum.kirupa.com/t/js-tip-of-the-day-symbol-tostringtag/643164>\
**Category:** web dev\
**Created:** [March 3, 2020, 2:12pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-symbol-tostringtag/643164 "2020-03-03T14:12:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [March 3, 2020, 2:12pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-symbol-tostringtag/643164/1 "2020-03-03T14:12:48Z")

</div>

**Symbol.toStringTag**  
Version: ES2015  
Level: Intermediate

You may recognize this iconic string: `[object Object]`. This will sometimes sneak into your output accidentally as its the default “toString” representation for basic objects in JavaScript.

```javascript
let basic = { data: 'GOTO 1' };
console.log(`The data I faxed was ${basic}.`);
// The data I faxed was [object Object].

```

If an object type doesn’t have its own custom `toString` implementation, the `toString` method from the Object type will get inherited and used when converting an object into a string. Its this version of `toString` which produces the output seen above. It follows the format `"[object ${tag}]"` where the `tag` value is the object’s “toString tag”.

The toString tag of an object can be determined a couple different ways, but the most direct way is to have a `Symbol.toStringTag` property in the object. If available, the value of this property is used by the default Object `toString` for it’s value of `tag`. If not, the default of “Object” is used.

```javascript
let basic = { data: 'GOTO 1', [Symbol.toStringTag]: 'BASIC' };
console.log(`The data I faxed was ${basic}.`);
// The data I faxed was [object BASIC].

```

The value of the `Symbol.toStringTag` property can be any string and can be in the object itself or inherited. Of course, I doubt our example in particular wants any version of the default `toString`, so it would be better off implementing its own custom version, skipping the need for a toString tag entirely.

```javascript
let basic = {
    data: 'GOTO 1',
    toString () {
        return `"${this.data}"`;
    }
};
console.log(`The data I faxed was ${basic}.`);
// The data I faxed was "GOTO 1".

```

More info:

- [Symbol.toStringTag on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag)
- [Determining tag in the ECMAScript spec](http://www.ecma-international.org/ecma-262/10.0/index.html#sec-object.prototype.tostring)
