How to use JavaScript tostring() Method? Solutions with Examples

Find out how to use the tostring() method effectively and efficiently in your JavaScript code.

In this article, you’ll discover and learn all about the JavaScript tostring() method, which is used to convert data to strings.

This article will help you master the art of converting data to strings from the basics to advanced usage using .tostring in JavaScript.

What is tostring in JavaScript?

The JavaScript tostring is used to return a string representing the specified object.

It is used internally by JavaScript when an object needs to be displayed as text or when an object needs to be used as a string.

Syntax

toString()

Parameters

The toString() method doesn’t require any parameters by default.

The tostring method is meant to be overridden by derived objects for custom type conversion logic.

How to override the toString() method

Here’s an example of overriding the toString() method:

function Person(name) {
  this.name = name;
}

const person1 = new Person("Itsourcecode");

Person.prototype.toString = function personToString() {
  return `${this.name}`;
};

console.log(person1.toString());

Output:

Itsourcecode

Here’s another example of overriding the toString() method in a custom class:

class Data {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }

     toString() {
        return `Data: ${this.name} ${this.age}`;
    }
}

const data = new Data("Itsourcecode", 18);
console.log(data.toString());

Output:

Data: Itsourcecode 18

In this example, we create a Data class with a name and an age property. Then, we override the toString() method to return a string representation of the object that includes the name and age in the given data.

When we create an instance of the Data class and call its toString() method, it returns the custom string representation that we defined.

What does .toString do in JavaScript?

The .toString() method is used to convert a value to its corresponding string representation. It can be called on various data types, such as numbers, arrays, objects, and more.

When used on primitive data types, such as numbers or booleans, the toString() method is implicitly invoked, and the conversion happens automatically.

Consequently, for complex data types like arrays and objects, developers have the flexibility to customize the conversion process by overriding the toString() method.

How to call toString in JavaScript?

You can call the toString() method on a value by using dot notation.

Here’s an example:

let num = 100;
let str = num.toString();✅
console.log(str);

Output:

100

In this example, we have a variable num with the value 100. Then, we call the toString() method on num using dot notation (num.toString()) and store the result in the variable str.

When we log the value of str to the console, we see that it is now a string with the value “100.”

Here’s an example with an array:

You can also call the toString() method on other data types, such as arrays and objects.

let arr = [1, 2, 3, 4, 5];
let str = arr.toString();
console.log(str); 

In this example, we have an array arr with the values [1, 2, 3, 4, 5]. Then, we call the toString() method on arr using dot notation (arr.toString()) and store the result in the variable str.

When we log the value of str to the console, we see that it is now a string with the following value:

Output:

"1,2,3,4,5"

Solutions on how to use object.tostring() method in JavaScript?

Here are some examples of using the toString() method on different types of objects:

Solution 1: Use toString() on an array

Here’s an example:

const subjects = ["Math", "English", "Science", "History"];
let text = subjects.toString();

console.log(subjects)

Output:

[ 'Math', 'English', 'Science', 'History' ]

Solution 2: Use toString() on an object

Here’s an example:

const person = {
firstName: "It",
lastName: "Sourcecode",
age: 18,
Address: "USA"
};
const keys = person.toString();

console.log(person);

Output:

{ firstName: 'It', lastName: 'Sourcecode', age: 18, Address: 'USA' }

Solution 3: Use Object.toString() on an object

Here’s an example:

const person = {
firstName: "It",
lastName: "Sourcecode",
age: 18,
Address: "USA"
};
const keys = Object.toString(person);
console.log(person);

Output:

{ firstName: 'It', lastName: 'Sourcecode', age: 18, Address: 'USA' }

Here’s another example of overriding toString for custom objects using Using Object.prototype.toString() on an object:

const person = {
firstName: "It",
lastName: "Sourcecode",
age: 18,
Address: "USA"
};

const keys2 = Object.prototype.toString.call(person);
console.log(keys2);

Output:

[object Object]

What is the JavaScript Number toString() Method

The JavaScript number toString() method is used to convert a given number into a string. It will return a string that represents the specified Number object.

Syntax

num.toString(base)

How to use .toString to convert number into a String?

When applying the tostring method on a number, it converts the number to a string representation.

For example of convert a number to a string:

let num = 18;
let str = num.toString();✅
console.log(str);

Output:

"18"

Here’s another example of using base 2 (binary) when converting a number to a string:

let num = 18;
let str = num.toString(2)
console.log(str);

Output:

10010

What’s the best way to convert a number to a string in JavaScript?

Here are the different ways to convert a number to a string in JavaScript. The following are common methods which you can easily use:

1. Using the toString() method

The toString() method takes an integer or floating point number and converts it into a string type.

For example:

let samplenum = 18;
let str = samplenum.toString();
console.log(str);

Output:

"15"

2. Using the String() function

The String() function creates a primitive string type for the number passed to it.

For example:

let samplenum = 19;
let str = String(samplenum);
console.log(str);

Output:

"19"

3. Using template literals

Template literals can put a number inside a string, which is a valid way of parsing an integer or float data type.

For example:

let num = 20;
let str = `${num}`;
console.log(str); 

Output:

20

4. Concatenating with an empty string

This method is considered one of the fastest ways of converting a number into a string.

For example:

let samplenum = 21;
let str = '' + samplenum;
console.log(str);

21

Conclusion

In conclusion, this article provides a detailed discussion on understanding the JavaScript .toString() method, which is used to convert data to strings.

We explain the syntax and usage of the method, including how to override it for custom type conversion.

The article also demonstrates how to call .toString() on various data types. Moreover, solutions are also provided on how to use .toString() on different types of objects and explore the JavaScript Number.toString() method for converting numbers to strings.

This article has multiple approaches to effectively and efficiently use the .toString() method in your JavaScript code.

We are hoping that this article provides you with enough information that helps you understand JavaScript tostring. 

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.
Caren Bautista

Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel  · View all posts by Caren Bautista →

Leave a Comment