Nerd For Tech
Published in

Nerd For Tech

Basics of Javascript · String · valueOf() (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”.

Hello my fellow developers! I am Jacob and this is the last method of the String built-in object series. It was not always easy to force myself to do the next article on time — especially because barely anyone watches them. But if you want to make it, you can not give up, so I didn’t and here we are. Today’s method is called valueOf() and it will be a nice end to our series. Let’s dive right in!

valueOf() method returns the primitive value of a String object.

It is basically equivalent to another String method called toString(). It does not have any input parameters, so it is automatically among my favourites.

Usually, this method is called internally by JavaScript itself and not explicitly in code. But that does not mean we will not test it in our example. Oh no! Quite the contrary. So let’s get to it.

Let’s see example 1.

// String object in variable
let strVar1 = new String("Hi Newbs!");
strVar1.valueOf() // Hi Newbs!
// primitive string value in variable
let strVar2 = "Hi Newbs!"; // Hi Newbs!
// explicitly declared string value
"Hi Newbs!".valueOf() // Hi Newbs!
// confirmation that all the results are equal in both value & type
strVar1.valueOf() === strVar2.valueOf() // true
strVar2.valueOf() === "Hi Newbs!".valueOf() // true
"Hi Newbs!".valueOf() === strVar1.valueOf() // 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 highlighted by the confirmation part of the example below, where I compared each combination of the 3 results. It is always equal in both value & type.

The one thing I want to mention is that you can not invoke the valueOf() method with anything you think of. If you use for example “null” or “undefined” they will throw TypeError. Keep that in mind.

try {
// TypeError: Cannot read property 'valueOf' of null
try {
// TypeError: Cannot read property 'valueOf' of undefined

And here it is. The last method of the String object finished. The only thing left is to thank you, if you kept watching and made it here to the very end of the series. But don’t be afraid — this is not the end really. I have my eyes locked on another object worth covering. It’s up to you to guess which one it is and for me to surprise you.

As always — thank you for your attention and your time spent watching me mumbling in english. I will see you soon with a new series of JS Basics.




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

Recommended from Medium

Create a CSS Pixel Art Generator with JavaScript

Introduction to Javascript part-2

HTML Crash Course for Beginners -4

Sharing Types Between Angular and Firebase Cloud Functions

What is the Floating-point Precision Error?

Animation In New Version Of Angular

I ❤ React JS Community

Event Bubbling and Event Capturing in React

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

“==” vs “===” in JavaScript

Create and Print Linked List in JavaScript

3 ways to split and remove HTML from a string

Basics of JavaScript and Version Controlling