bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/JavaScript/JavaScript Foundations
JavaScript•JavaScript Foundations

JavaScript toString()

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind JavaScript toString()?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ fruits = ["Banana", "Orange", "Apple", "Mango"];
3Order

Put the learning moves in the order that makes the concept easiest to apply.

JavaScript Date toString()
JavaScript Array toString()
JavaScript toString()

The JavaScript toString() method converts a variable (or a value) to a string.

It is a built-in method for many data types, including numbers, arrays, dates, and objects.

The method is useful for

  • Converting data to a readable format for display
  • Ensuring type compatibility when a string is required
  • Customizing objects other user interfaces
  • Customizing objects for debugging

JavaScript Array toString()

When used on an array, toString() returns the array elements as a comma separated string.

Example

const fruits = ["Banana", "Orange", "Apple", "Mango"];
let myList = fruits.toString();

JavaScript Date toString()

When used on a date, toString() returns a human-readable date and time string.

Example

const d = new Date();
let text = d.toString();

JavaScript Number toString()

When used on a number, toString() returns the number as a string.

Example

let x = 123;
let text = x.toString();

Convert a number to a string, using base 2 (binary):

Example

let x = 123;
let text = x.toString(2);

JavaScript Function toString()

When used on a function, toString() returns the source code of the function as a string.

JavaScript Object toString()

When used on an object, toString() an object returns "[object Object]".

To provide a meaningful string representation, it can be overridden in the object definition:

Example

let person = {
  firstname: "John", lastname: "Doe", }
let text = person.toString();

Previous

JavaScript Function Return

Next

JavaScript Const