Home > Web Front-end > JS Tutorial > body text

JavaScript method toString() converts a logical value into a string and returns the result

黄舟
Release: 2017-11-04 13:19:37
Original
2184 people have browsed it

Definition and usage

toString() method can convert a logical value into a string and return the result.

Syntax

booleanObject.toString()
Copy after login

Return value

Returns the string "true" or "false" based on the original Boolean value or the value of the booleanObject object.

Throws

If the object calling this method is not a Boolean, Throws an exception TypeError.

Tips and Notes

Note: This method is automatically called when a Boolean object is used in a string environment.

Example

In this example, we will create a Boolean object and convert it to a string:

<script type="text/javascript">
 
 var boo = new Boolean(true)
document.write(boo.toString())

</script>
Copy after login

Output:

true
Copy after login

Example &Instructions

The following example runs in a host environment of Windows 7 Simplified Chinese Ultimate Edition 64-bit, located in mainland China. Depending on the locale and language settings, the output results of the execution may be different.

//数组
var array = ["CodePlayer", true, 12, -5];
document.writeln( array.toString() ); // CodePlayer,true,12,-5

// 日期
var date = new Date(2013, 7, 18, 23, 11, 59, 230);
document.writeln( date.toString() ); // Sun Aug 18 2013 23:11:59 GMT+0800 (中国标准时间)

// 日期2
var date2 = new Date(1099, 7, 18, 23, 11, 59, 230);
document.writeln( date2.toString() ); // Fri Aug 18 1099 23:11:59 GMT+0800 (中国标准时间)

// 数字
var num =  15.26540;
document.writeln( num.toString() ); // 15.2654

// 布尔
var bool = true;
document.writeln( bool.toString() ); // true

// Object
var obj = {name: "张三", age: 18};
document.writeln( obj.toString() ); // [object Object]

// HTML DOM 节点
var eles = document.getElementsByTagName("body");
document.writeln( eles.toString() ); // [object NodeList]
document.writeln( eles[0].toString() ); // [object HTMLBodyElement]
Copy after login


The above is the detailed content of JavaScript method toString() converts a logical value into a string and returns the result. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template