Open In App

TypeScript | String valueOf() Method

Last Updated : 19 Jul, 2024
Comments
Improve
Suggest changes
44 Likes
Like
Report

The String.prototype.valueOf() method is an inbuilt function in TypeScript that returns the primitive value of a String object. This method is useful when you need to obtain the raw string value from a String object.

Syntax: 

string.valueOf( ) 

Parameter: This method does not accept any parameter. 

Return Value: This method returns the primitive value of a String object. 

Below examples illustrate the String valueOf() method in TypeScript

Examples of String valueOf() Method

Example 1: Basic Usage of valueOf()

In this example, we will use the valueOf() method to get the primitive value of a String object.

const strObj: String = new String("Geeksforgeeks - Best Platform");
const primitiveValue: string = strObj.valueOf();

console.log(primitiveValue);

Output:  

Geeksforgeeks - Best Platform

Example 2: Another Usage of valueOf()

In this example, we will demonstrate another usage of the valueOf() method.

const strObj: String = new String("TypeScript - String valueOf()");
const primitiveValue: string = strObj.valueOf();

console.log(primitiveValue); 

Output: 

TypeScript - String valueOf()

Next Article

Similar Reads