Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

Converting An Object to String in JavaScript

There are three ways to convert an object to string in Javascript will be as below:

 
1. value.toString() 
2. value+"" 
3. String(value) 

Limitation to Use

value.toString() can only be use when the value is not null or undefine.

value+”” can only be use when the value is non empty string.

String(value) this way looks like a function call which can be confusing especially to those who are coming from Java. For this case, String will be a constructor instead of a function. Output of this will be different from a  function.

For value.toString(), .toString() will convert primitive to string as below:

Argument Result
undefined "undefined"
null "null"
boolean value either "true" or "false"
number value the number as a string, e.g. "1.765"
string value no conversion necessary

Conclusion

Which approaches do you prefer to use?

If you know that value is not null or undefine, you can use  value.toString(). Otherwise,  ""+value and String(value) are mostly equivalent.

 

One thought on “Converting An Object to String in JavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *

×