How to get the primitive value of string in Javascript?


In JavaScript if the data is not an object and has no methods and properties is called as a primitive data type or a primitive value. The different types of primitive data types in JavaScript are string, number, Boolean, undefined, symbol, null and big int.

The primitive types boolean, string and number can be wrapped by their wrapper object, i.e., instances of the Boolean, String and Number constructors respectively.

To get the back the primitive values back from the object wrappers, we need to call the valueOf() method on the objects.

Example

Following example demonstrates the primitive and object types of boolean, string and number −

console.log("boolean :::::"); console.log("type of 'true': "+typeof true); console.log("type of 'new Boolean()': "+typeof new Boolean()); console.log("value of 'new Boolean(true)': "+new Boolean(true).valueOf()); console.log("string :::::"); console.log("type of 'abc': "+typeof "abc"); console.log("typeof 'new String()': "+typeof new String("abc")); console.log("value of 'new String('abc')': "+new String("abc").valueOf()); console.log("number :::::"); console.log("type of '123': "+typeof 123); console.log("type of 'new Number()': "+typeof new Number(123)); console.log("value of 'new Number(123)': "+new Number(123).valueOf());

Primitive values of a String

The primitive value of a string is returned by the method valueOf() as a string data type.

However, for objects of type Number, the function valueOf() { [native code] }() returns the primitive numeric value represented by the object. Similarly, it returns the primitive Boolean value of a Boolean object and the string of a String object.

It is rarely necessary to call this function directly. When an object is used in a place where a primitive value is expected, JavaScript does this automatically.

The automatic invocation of the function valueOf() { [native code] }(), distinguishing between primitive values and their corresponding objects is difficult. The typeof() operator distinguishes between strings and String objects, for example, but in practise, you can use them interchangeably in JavaScript code.

Syntax

The valueof() syntax is as follows −

stringName.valueOf()

The valueOf() method doesn’t take any parameters and returns the primitive value of the calling String object i.e., the string itself. As it returns the string that means it doesn’t change the object to a string it just returns the string of the given object.

Example 1

Following is an example of the valueOf() function. In here we are retrieving the value of a Sting object −

var str = new String('You are viewing this content in Tutorials Point Website'); console.log("The given input object with its type is:",str) console.log(typeof(str)); console.log("The primitive value of the given string object with its type is:",str.valueOf()) console.log(typeof(str.valueOf()));

Example 2

Let us see another example for this −

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>valueOf()</title> </head> <body> <h3>The valueOf() Method - Strings</h3> <p>The valueOf() method will return the primitive value of the string in JS</p> <p id="d"></p> <script> let tp = new String("This tutorial is about valueOf() method and primitive values"); let res = tp.valueOf() document.getElementById("d").innerHTML = res +"<br/>" + typeof(tp) +" returned as: " + typeof(res); </script> </body> </html>

Updated on: 26-Aug-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements