Home Web Front-end JS Tutorial Introduction to object conversion functions toString() and valueOf()

Introduction to object conversion functions toString() and valueOf()

May 25, 2017 pm 02:49 PM
javascript object tostring() valueof() conversion function

In JavaScript, the operation of converting object to boolean is very simple: all objects are true after being converted to boolean; even objects such as new Boolean(false) are still true after being converted to boolean.

Copy code The code is as follows:

var x = new Boolean(false);if(x){  console.log("x is true");}
Copy after login

When converting object to string or number, JavaScript will call two conversions of object Functions: toString() and valueOf().

toString()

The toString() function is to return the string representation of object. The default toString() method of object in JavaScript returns a string "[object Object]". When defining a class, you can implement the new toString() method to return more readable results. JavaScript defines a more readable toString() method for array objects, function objects, regular expression objects, and Date date objects:

1.array’s toString() method will return comma-separated Array members. For example, [1,2,3].toString() will return the string "1,2,3".

2. The toString() method of function will return the text definition of the function. For example, (function(x){return x*2;}).toString() will return the string "function(x){return x*2;}".

3.RegExp’s toString() method is similar to function’s toString() method and will return the text definition of the regular expression. For example, /d /g.toString() will return the string "/\d /g".

4.Date’s toString() method will return a readable date and time string.

valueOf()

The valueOf() function is to return the numerical representation of the object. The default valueOf() method of object in JavaScript will return the object itself. Like toString(), you can implement a new valueOf() method when defining a class to return the desired result. JavaScript defines a more readable valueOf() method for the Date object:

The valueOf() method of Date will return a value, which is the time difference between the Date object and 0:00 on January 1, 1970 (in milliseconds).

Related recommendations]

1. Detailed explanation of valueOf method examples in java

2. Between valueOf and toString, (String) in Java The difference between

3. Usage of tostring() and valueof() and the difference between the two_

4.valueOf function and toString In-depth understanding of the method

5. The difference between valueOf, parseInt, and toString in Java

6. Using the toString() method to return the time is String

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to implement an online speech recognition system using WebSocket and JavaScript

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to implement an online reservation system using WebSocket and JavaScript

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

How to use JavaScript and WebSocket to implement a real-time online ordering system

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

Simple JavaScript Tutorial: How to Get HTTP Status Code

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecasting system

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

How to use insertBefore in javascript

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

How to get HTTP status code in JavaScript the easy way

See all articles