JavaScript Symbol.toStringTag Properties - JavaScript Tutorial

JavaScript Symbol.toStringTag Properties

BLUF: This tutorial on JavaScript Symbol.toStringTag Properties provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: JavaScript Symbol.toStringTag Properties

Understanding JavaScript Symbol.toStringTag Properties is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

The Symbol.toStringTag symbol in JavaScript is utilized internally by the Object.prototype.toString function. This symbol plays a crucial role in generating the standard string representation of an object.

Syntax

Example

Symbol.toStringTag

Parameters

String

Return value

String Object.

Browser Support

Chrome 32
Safari 8
Firefox 29
Opera 19

Example 1

Example

<script>

//JavaScript to illustrate Symbol.toStringTag

class ToString {get [Symbol.toStringTag]() {

   return 'Example';

  }

}

document.write(Object.prototype.toString.call(new ToString()));

//expected output: [object Example]

</script>

Output:

Output

[object Example]

Example 2

Example

<script>

//JavaScript to illustrate Symbol.toStringTag

document.write(Object.prototype.toString.call(([1,2,3])));

document.write("<br>");

document.write(Object.prototype.toString.call((true)));

//expected output: [object Array]

//[object Boolean]

</script>

Output:

Output

[object Array]

[object Boolean]

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience