Using String valueOf() method to coding in Javascript/JS

This JavaScript tutorial explains how to use the string technique known as valueOf() with syntax and examples.

Description

In JavaScript, valueOf() is a string method that is used to return the primitive fee or string representation of an object. Because the valueOf() method is a technique of the String object, it ought to be invoked through a unique occasion of the String class.

Syntax

In JavaScript, the syntax for the valueOf() technique is:

string.valueOf();

Parameters or Arguments

There are no parameters or arguments for the valueOf() method.

Returns

The valueOf() technique returns the primitive price of the calling String object.

Note

The valueOf() approach does not trade the fee of the unique string. You can also use the toString() method to return the string illustration of an object.

Example

Let’s take a appear at an instance of how to use the valueOf() technique in JavaScript.

For example:

var totn_string = new String('TechOnTheNet');

console.log(totn_string);
console.log(totn_string.valueOf());

In this example, we have declared a string object called totn_string that is assigned the cost of ‘TechOnTheNet’. We have then invoked the valueOf() method of the totn_string variable to return a string illustration of the object.

We have written the output of the string object as properly as the valueOf() method to the net browser console log, for demonstration purposes, to show what the valueOf() approach returns.

The following will be output to the net browser console log:

String {"TechOnTheNet"}
TechOnTheNet

In this example, the first output to the console log indicates the String object. Whereas, the 2nd output to the console log indicates the primitive price or the string representation of the object (which is ‘TechOnTheNet’).