Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert instance of any type to string?

Tags:

javascript

I'm implementing a function that receives an argument which it needs to convert to its string representation.

If a given object implements a toString() method, then the function should use it. Otherwise, the function can rely on what the JavaScript implementation offers.

What I come up with is like this:

var convert = function (arg) {   return (new String(arg)).valueOf(); } 
like image 704
beatak Avatar asked May 15 '09 16:05

beatak


People also ask

How do I convert an instance to a string in Python?

Converting Object to String Everything is an object in Python. So all the built-in objects can be converted to strings using the str() and repr() methods.

How do you turn an object into a string?

Convert Object to String in java using toString() method of Object class or String. valueOf(object) method. Since there are mainly two types of class in java, i.e. user-defined class and predefined class such as StringBuilder or StringBuffer of whose objects can be converted into the string.

How do you turn an object into a string in JavaScript?

Stringify a JavaScript ObjectUse the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);

How do I convert an object to a string in Salesforce?

String jsonStr = JSON. serialize(obj);


1 Answers

String(null) returns - "null"

String(undefined) returns - "undefined"

String(10) returns - "10"

String(1.3) returns - "1.3"

String(true) returns - "true"

I think this is a more elegent way.

like image 72
Chang Avatar answered Sep 22 '22 02:09

Chang