最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why would you use string.toString()? - Stack Overflow

programmeradmin1浏览0评论

I'm trying to understand why you would ever use toString() on a string.

tutorialspoint gives this example.

<html>
  <head>
    <title>JavaScript String toString() Method</title>
  </head>

  <body>

    <script type="text/javascript">
      var str = "Apples are round, and Apples are Juicy.";
      document.write(str.toString( ));
    </script>

  </body>
</html>

Why not just use

document.write(str);

I'm trying to understand why you would ever use toString() on a string.

tutorialspoint.com gives this example.

<html>
  <head>
    <title>JavaScript String toString() Method</title>
  </head>

  <body>

    <script type="text/javascript">
      var str = "Apples are round, and Apples are Juicy.";
      document.write(str.toString( ));
    </script>

  </body>
</html>

Why not just use

document.write(str);
Share Improve this question asked Jun 21, 2016 at 18:01 user1757006user1757006 7753 gold badges13 silver badges25 bronze badges 5
  • 2 it's just an example.... actually there is nothing to do with toString().... Also there is description about the return value Returns a string representing the specified object. . Also I would always prefer MDN documentation – Pranav C Balan Commented Jun 21, 2016 at 18:04
  • 1 poorly docs, take a look to a real doc – elreeda Commented Jun 21, 2016 at 18:04
  • They only want to explain toString method of object, which string is object – ThiepLV Commented Jun 21, 2016 at 18:05
  • 1 if you didn't know for sure it was a string, toString() will turn it into one... – dandavis Commented Jun 21, 2016 at 18:41
  • I think this is a good, deep, important question and I thank you for asking it!! I’m very curious too, I was just googling. My guess is something like: especially in weakly typed languages, it may make sense to implement even degenerate and trivial functions to ensure they exist for when you don’t know a priori that it’s gonna be degenerate and trivial, like when you don’t actually know the type of str. Which feels “impure” and practical, and so, very javascripty. – Toph Commented Jan 10, 2018 at 16:41
Add a comment  | 

4 Answers 4

Reset to default 8

toString() method doesn't make any practical sense when called on a "pure" sring, like "Apples are round, and Apples are Juicy.".
It may be useful when you need to get the string representation of some non-string value.

// Example:
var x = new String(1000);   // converting number into a String object

console.log(typeof x);             // object
console.log(typeof x.toString());  // string
console.log(x.toString());         // "1000"

The String object overrides the toString() method of the Object object; it does not inherit Object.prototype.toString(). For String objects, the toString() method returns a string representation of the object and is the same as the String.prototype.valueOf() method.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toString

string.toString() is useful if you are not entirely certain wether you might get a Number or a String, and if you definitely need a string to proceed with other calculations, e.g. String functions like .includes or .split.

Even if you don't use the toString function, it is invoked (implicitly) to get the value that is used for the document.write() function.

There's no difference. I think that they used the toString function just to emphasize that it exists. Every object in javascript has a toString function, because it's defined on Object's prototype, like explained here.

The difference is that the String type overrides the original toString function, otherwise the result would be "[object String]".

发布评论

评论列表(0)

  1. 暂无评论