Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use toString () function and valueOf () function in js

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to use the toString () function and valueOf () function in js". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. Preface

In the equals operator, if the comparison contains object type data, implicit conversion is involved, then the toString () function and the valueOf () function are called.

Second, toString () function

The toString () function converts a logical value to a string and returns the result.

Console.log ((1) .toString ()); / 1console.log ((10) .toString (2)) / / 1010console.log (("1") .toString (); / / 1console.log ((false) .toString ()); / / falseconsole.log (({pv1}) .toString ()); / / [object,object] console.log ((undefined) .toString ()); / / error console.log ((null). ToString ()) / / report an error console.log ((function () {}). ToString ()); / / function () {} console.log ([1JI 2je 3je 4] .toString ()); / / 1Jing 2Jing 3Jing 4console.log ((new Date ()). ToString ()) / / Fri Jul 03 2020 17:20:11 GMT+0800 (China Standard time)

In JavaScript, types such as Object,Array,Function,Date implement a custom toString () function.

The default return result of the toString () function of Object type data is "[object Object]". When we customize the new class, we can override the toString () function to return more readable results.

The toString () function of Array returns a comma-separated array member string. An array is also an object, so why does the array return the corresponding string instead of the object? in fact, the array overrides the Object.toString method, and then concatenates the array and returns a string containing each array element separated by a comma (equivalent to Array.join ())

The toString () function of Function returns a text definition of the function

The toString () function of Date returns a readable time string

Third, valueOf () function

The valueOf () function returns the original value that best fits the reference type, or the reference type itself if there is no original value.

Console.log ((1). ValueOf ()); / 1console.log ((10) .valueof (2)) / / error console.log (("1"). ValueOf (); / / 1console.log ((false). ValueOf ()); / / falseconsole.log (({plaz1}). ValueOf ()); / / {console.log ((undefined). ValueOf ()); / / error console.log ((null). ValueOf ()) / / report an error console.log ((function () {}). ValueOf ()); / / function () {} console.log ([1mai 2je 3je 4] .valueOf ()); / / [1Med 2Med 3je 4] console.log ((new Date ()) .valueOf ()); / / 1593767848260

The default return result of the valueOf () function of Object type data is "{}", which is an empty object literal.

The valueOf () function of Array returns the array itself

The valueOf () function of function returns the function itself

The valueOf () function of Date returns the timestamp of the specified date

Summary:

ToString () returns a string, while valueOf () returns the original value, and no original value returns the object itself

Neither undefined nor null has toString () and valueOf () methods

A string representing the time returned by toString () of type Date; valueOf () returns the number of milliseconds (timestamp) between now and January 1, 1970

The toString () method of type Number can accept the conversion cardinality and return numeric values in the form of strings with different digits, while the valueOf () method cannot accept the conversion cardinality.

IV. Special circumstances

If there is both a toString () function and a valueOf () function for the value of a reference type, which function will be called when making an implicit conversion? Here we can summarize two scenarios, namely, the conversion of reference types to String types and the conversion of reference types to Number types.

1. Convert reference type to String type

Data of a reference type is generally used for data display when converted to String type, and the following rules are followed during conversion:

If the object has a toString () function, the toString () function is called first. If it returns an original value, the original value is directly converted to a string representation and the string is returned.

Otherwise, the valueOf () function is called again, and if the result returned by the valueOf () function is an original value, the result is converted to a string representation and the string is returned.

If you cannot get an original value through either the toString () function or the valueOf () function, a type conversion exception is thrown directly.

2. Convert reference type to Number type

When data of a reference type is converted to Number type, it is generally used for data operation, and the following rules are followed during conversion:

If the object has a valueOf () function, the valueOf () function is called first, and if the valueOf () function returns an original value, the original value is directly converted to a numerical representation and the number is returned.

Otherwise, the toString () function is called again, and if the result returned by the toString () function is an original value, the result is converted to a numerical representation and the number is returned.

If you cannot get an original value through either the toString () function or the valueOf () function, a type conversion exception is thrown directly.

This is the end of the content of "how to use the toString () function and valueOf () function in js". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report