Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript Object Prototypes
Next article icon

JavaScript Object.prototype.toString() Method

Last Updated : 04 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

In JavaScript, the  Object.prototype.toString() method is used to return a string that can represent the object. The toString() method is automatically inherited by every object which is inherited from Object.  Whenever an object is represented as a text value or a string is expected from the object, the toString() method is called automatically.  

Syntax:

obj.toString()

If one does not override the toString() method in case of custom objects, the toString() method returns the following:

[object type] 

In the above syntax, the type denotes the object type.

Another use of the toString() method is that it can be used to convert base 10 numbers (and even bigInts) to other base numbers. 

Syntax:

ExNum.toString(radix);

In the above syntax, the ExNum is an object of the object type number or bigInt and the radix refers to the base the number to is be converted to.

Example 1: The following example shows how the toString() method works when dealing with default object type and custom object type when the toString() method is not overridden.

JavaScript
// Creating a custom object type function ExObjType(n) {     this.number = n; }  function myFunction() {     // Creating an object of the     // String class     const o = new String('Hello');     console.log(o.toString());      // Creating an object ExObj1 of     // custom defined class       const ExObj1 = new ExObjType(3);      // Returns [object Object] as toString()     // not overridden for custom class     console.log(ExObj1.toString()); } myFunction(); 

Output:

Hello  [object Object]

 Example 2: The following example shows how the toString() method works when dealing with custom object type and the toString() method is overridden  :

JavaScript
// Creating a custom object type function ExObjType(n) {     this.number = n; }  // Overriding the toString() method // for ExObjectType ExObjType.prototype.toString = function ExObjToString() {     const ans = 'The number related to this object is '     + this.number;     return ans; }  function myFunction() {     // Creating an object ExObj1 of      // custom defined class       const ExObj1 = new ExObjType(3);      // Calling the toString() method     // after overriding     console.log(ExObj1.toString()); } myFunction(); 

Output:

The number related to this object is 3

 Example 3: The following example shows how to use the toString() method to convert base 10 numbers to different base numbers.

JavaScript
function myFunction() {     // Creating a constant num1     // with value 12     const num1 = 12;      // Converts num1 to a binary form     console.log(num1.toString(2));      // Converts num1 to its octal form     console.log(num1.toString(8));      // Converts num1 to its base-5 form     console.log(num1.toString(5)); } myFunction(); 

Output:

1100  14  22

We have a complete list of Javascript Object methods, to check those please go through this Objects in JavaScript article.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.  


Next Article
JavaScript Object Prototypes

A

ashutoshrathi
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-object
  • JavaScript-Methods

Similar Reads

  • JavaScript Object setPrototypeOf() Method
    The Object.setPrototypeOf() method in JavaScript is a standard built-in object thatthat will sets the prototype (i.e., the internal [[Prototype]] property) of a specified object to another object or null. Syntax:Object.setPrototypeOf(obj, prototype)Parameters:This method accepts two parameters as me
    3 min read
  • JavaScript Error.prototype.toString() Method
    The Error.prototype.toString() method is an inbuilt method in JavaScript that is used to return a string representing the specified Error object. Syntax: e.toString() Parameters: This method does not accept any parameters. Return value: This method returns a string representing the specified Error o
    1 min read
  • JavaScript Object.prototype.valueOf() Method
    In JavaScript, the Object.prototype.valueOf() method is used to return the primitive value of the specified object. Whenever a primitive value is required, JavaScript automatically invokes the valueOf() method. The valueOf() method is automatically inherited by every object in JavaScript. Every obje
    2 min read
  • JavaScript Number toString() Method
    The toString() method in JavaScript returns a number as a string. It allows converting numerical values to string representations, enabling operations like formatting output, displaying numbers in various formats, and facilitating string manipulation based on numeric values. Syntax: num.toString(bas
    2 min read
  • JavaScript Object Prototypes
    JavaScript prototypes are used to access the properties and methods of objects. Inherited properties are originally defined in the prototype or parent object. The Date object is inherited from Date.prototype, Array object inherits from Array.prototype, etc. The prototypes may be used to add new prop
    1 min read
  • JavaScript String prototype Property
    The prototype property allows to add new properties and methods to the existing JavaScript object types. There are two examples to describe the JavaScript String prototype property. Syntax: object.prototype.name = valueReturn Value: It returns a reference to the String.prototype object. Example 1: T
    2 min read
  • JavaScript Reflect setPrototypeOf() Method
    JavaScript Reflect.setPrototypeOf() method in JavaScript is used to set the prototype of a specified object to another object or to Null. This method returns the boolean value for the operation that is successful. This method is the same as the Object.setPrototypeOf() method. Syntax: Reflect.setProt
    2 min read
  • JavaScript String toString() Method
    The toString() method in JavaScript converts a string object to a string primitive. It returns a string representing the specified object. This method is automatically called when a string object needs to be defined as a text value or concatenated with another string. The toString() method also conv
    3 min read
  • JavaScript Symbol toString() Method
    The symbol.toString() is an inbuilt method in JavaScript that is used to convert the specified symbol object into the string. Syntax: Symbol().toString(); Here Symbol() is the specified symbol object which is to be converted into a string. Parameters: This method does not accept any parameter. Retur
    1 min read
  • JavaScript Object.prototype.__defineSetter__() Method
    The __defineSetter__() method is used to bind an object's property to a function which will be called when an attempt is made to set the specified property. It is recommended to use the object initializer syntax or the Object.defineProperty() API instead of this method as it is being deprecated. Syn
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences