str.toString() returns [object Object]

javascript object tostring
object to string javascript
tostring java
tostring js
tostring() c#
javascript class tostring
object to string c#
number prototype tostring

When i do str.replace(/\n/g, '')

I'm getting the error, that replace is not a function.

When i do str.toString() it returns [object Object]

is my String being treated like a JSON object or what? my string is a website body.

the string is assigned by making a GET request to a URL.

str.toString() returns "[object Object]" becouse your str var is a object

str = {};
str.toString();

$ "[object Object]"

Try tu debug your var with a console function console.log(str), this will probably return the entire object tree.


If you are looking for HTML content you can use the innerHTML attribute

str.innerHTML

prototype.toString() . For String objects, the toString() method returns a string representation of the object and is the same as the  15.2.4.2 Object.prototype.toString ( ) When the toString method is called, the following steps are taken: If the this value is undefined, return "[object Undefined]". If the this value is null, return "[object Null]". Let O be the result of calling ToObject passing the this value as the argument.

That's because str variable contains Object instead of String. You can make console.log(str) to see what value does it have.

toString() method interacts with objects. You'll also find that trying to put an object in a string (such as console.log('hello', object) ) results in the same thing. The toString () method returns the value of a String object. Browser Support. string .toString () Technical Details. A String, representing the value of a string. JavaScript Version: Previous JavaScript String Reference Next

You stated that str was the "website body". I am assuming what you did wrong is you probably wrote

var str = document.body; // Returns Node

instead of

var str = document.body.innerHTML; // Returns String

The reason is that [object Object] is the string version of a JSON object (or a Node):

var str = {};
console.log(str.toString());

Can someone tell me why toString() helper function returns [object Object] instead of a string representation of an object? Here's the code: The java.lang.Object.toString() method returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all

Have you ever wondered why if you try to alert a plain old JavaScript object, you get They are each providing a string representation of their values, but their toString = function () { return "rgb(" + this.r + ", " + this.g + ", " + this.b + ")"; }; var red​  Object.ToString is the major formatting method in the .NET Framework. It converts an object to its string representation so that it is suitable for display. (For information about formatting support in the .NET Framework, see Formatting Types .) Default implementations of the Object.ToString method return the fully qualified name of the object

“Every object has a toString() method that is automatically called when value or when an object is referred to in a manner in which a string is expected. have a customized toString method that prints a different return value,  Return value. A string representing the object. Description. Every object has a toString() method that is automatically called when the object is to be represented as a text value or when an object is referred to in a manner in which a string is expected. By default, the toString() method is inherited by every object descended from Object.

Returns the result of calling toString for a non- null argument and "null" for a null argument. static String · toString(Object o, String nullDefault). Returns the result of​  The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString

Comments
  • Why did i get downvoted..?
  • How exactly does str get its value?
  • str.replace('/\n/g, '') is not correct tho, should be str.replace(/\n/g, '')
  • Also you are not saving str variable after replacing it. You should save it str = str.replace()
  • "Why did i get downvoted..?" Because your question is not complete. You don't show how str is assigned, so we can't really see what you are actually doing wrong here.
  • innerHTML isn't a function; rather an attribute. Remove innerHTML() and replace it with innerHTML with no brackets
  • str is a website body.
  • @BobJensen it is an object. What this var should contain? What do you want to achieve?
  • to check if it contains a substring
  • @BobJensen if you want to get all text from your page your str assignment should be this: const str = document.body.innerText;