Nerd For Tech
Published in

Nerd For Tech

Basics of Javascript · String · toString() (method)

This article is a transcript of my free youtube series about basics of web development. If you prefer watching over reading, feel free to visit my channel “Dev Newbs”.

Hi there my fellow developers! I am Jacob and the method for today is called toString(). In some cases, you will ask yourself: “Why do I even need this?”, but other times it will actually be useful and you will be like: “Ah, right…because of this.” So let’s figure out together what that “this” really is.

toString() method returns a string representing the specified object. The String object actually overrides the method of the same name from the Object object. This means that the original toString() method from the object Object is not inherited and the String version of the method is used instead.

To understand the difference, I need to explain what is the result of invoking the original method from the Object object. By default, the toString() method is inherited by every object descended from Object. If this method is not overridden in a custom object, toString() returns “[object type]”, where type is the object type.

However for String objects, the toString() method returns a string representation of the object and the outcome is the same as if we used the valueOf() method, which BTW will be covered as the last method of the String object.

Let’s check it all in example 1.

// String object in variable
let strVar1 = new String("Hi Newbs!");
strVar1.toString() // Hi Newbs!
// primitive string value in variable
let strVar2 = "Hi Newbs!";
strVar2.toString() // Hi Newbs!
// explicitly declared string value
"Hi Newbs!".toString() // Hi Newbs!
// confirmation that all the results are equal in both value & type
strVar1.toString() === strVar2.toString() // true
strVar2.toString() === "Hi Newbs!".toString() // true
"Hi Newbs!".toString() === strVar1.toString() // true

First three cases show that it doesn’t really matter how you specify your string. It can be a String object variable or primitive string value or even explicit value that invokes the method. The results are still the same as long as the string value is the same.

That is confirmed by the confirmation part of the example below, where I compare each combination of the 3 results we get. It is always equal in both value & type.

The other types of built-in objects have their own version of the method and that’s exactly what the second example shows. Along with some error cases as a bonus.

let o = new Object();
o.toString() // [object Object]
let a = new Array(1, 2, 'yes', 'false', -45.47);
a.toString() // 1,2,yes,false,-45.47
let d = new Date();
d.toString() // Wed Jun 23 2021 08:45:58 GMT+0200 ...
let r = new RegExp();
r.toString() // /(?:)/
try {
console.log(null.toString());
}
catch(err){
console.log(err);
}
// TypeError: Cannot read property 'toString' of nulltry {
console.log(undefined.toString());
}
catch(err){
console.log(err);
}
// TypeError: Cannot read property 'toString' of undefined

By default, the Object method toString() returns a type of object within square brackets. Other objects like Array, Date or RegExp have their own overridden implementation of the method that returns something else. For example in the case of Array, the whole array is returned as one big string. Date returns current date time in a specific format. And it goes on and on. You can try each built-in object yourself, if you are into that kind of stuff.

The last thing I want to mention is that you can not invoke the toString() method with “null” or “undefined” as they are special kinds of snowflakes that just throw TypeError. Best to keep that in mind.

Alrighty! That was the String object method toString() and this is me thanking you for sticking with me till the end of this episode. I will see you soon with the next one.

--

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

Testing NodeJS code with Jest

How To Make a Discord Bot with Javascript!

Let’s talk about some confusing things about Javascript

confusing

Filtering Data in React Query Like a Pro

10 UI & UX tips for developers building SaaS apps

Anthem Walkthrough — TryHackme

What I have learned in the first year of my Job field

How to Show a Loader Until the Fetch API Has Finished Loading the Page

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jakub Korch

Jakub Korch

Web enthusiast, programmer, husband and a father. Wannabe entrepreneur. You name it.

More from Medium

How to cancel a Fetch request during in-flying in ReactJS

Application Development Principles and JavaScript

Must know principles for an Object Oriented Programmer and an introduction for JavaScript

Stack Data Structure and What does it have to do with JavaScript?