JavaScript String valueOf() method

Definition and usage

valueOf() The method returns the original value of the string.

valueOf() The method does not change the original string.

valueOf() The method can be used to convert string objects to strings.

See also:

toString() method

Note

valueOf() The method is the default method of JavaScript string.

JavaScript 在内部使用它。

通常,您不会在代码中使用它。

Instance

Example 1

Get the value of the text:

let text = "Hello World!";
let result = text.valueOf();

Try it yourself

It is equivalent to:

let text = "Hello World!";
let result = text;

Try it yourself

Example 2

Get the value of the String object:

let text = new String("Hello World!");
let result = text.valueOf();

Try it yourself

Syntax

string.valueOf()

Parameters

No parameters.

Return value

Type Description
String The original value of the string.

Throws

Exception Description
TypeError Throws an exception when the object called is not a String.

Browser support

valueOf() Is ECMAScript1 (ES1) feature.

All browsers support ES1 (JavaScript 1997):

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
支持 支持 支持 支持 支持 支持

相关页面

JavaScript Ɗanmu

JavaScript Ɗanmu Ɗaɗa

JavaScript Ɗanmu Ɗawo