Actually, the best solution is using JSON:
Documentation
JSON.parse(text[, reviver]);
Examples:
1)
var myobj = JSON.parse('{ "hello":"world" }');
alert(myobj.hello); // 'world'
2)
var myobj = JSON.parse(JSON.stringify({
hello: "world"
});
alert(myobj.hello); // 'world'
3) Passing a function to JSON
var obj = {
hello: "World",
sayHello: (function() {
console.log("I say Hello!");
}).toString()
};
var myobj = JSON.parse(JSON.stringify(obj));
myobj.sayHello = new Function("return ("+myobj.sayHello+")")();
myobj.sayHello();
Answer from Matej on Stack Overflow Top answer 1 of 16
234
Actually, the best solution is using JSON:
Documentation
JSON.parse(text[, reviver]);
Examples:
1)
var myobj = JSON.parse('{ "hello":"world" }');
alert(myobj.hello); // 'world'
2)
var myobj = JSON.parse(JSON.stringify({
hello: "world"
});
alert(myobj.hello); // 'world'
3) Passing a function to JSON
var obj = {
hello: "World",
sayHello: (function() {
console.log("I say Hello!");
}).toString()
};
var myobj = JSON.parse(JSON.stringify(obj));
myobj.sayHello = new Function("return ("+myobj.sayHello+")")();
myobj.sayHello();
2 of 16
95
Your string looks like a JSON string without the curly braces.
This should work then:
obj = eval('({' + str + '})');
WARNING: this introduces significant security holes such as XSS with untrusted data (data that is entered by the users of your application.)
Videos
14:36
Converting Objects to Strings with toString() - Advanced JavaScript ...
06:04
How to Convert a JavaScript Object to a String While ...
02:45
How to Convert JavaScript Object to JSON String? - YouTube
01:39
How to Convert a JavaScript Object to a String While Preserving ...
06:25
How to Parse JSON Data in JavaScript | Convert JSON Strings to ...
06:27
How to Parse JSON Data in JavaScript | Learn JSON.parse() to Read ...
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › toString
Object.prototype.toString() - JavaScript | MDN
... class Dog { constructor(name, ... code in place, any time an instance of Dog is used in a string context, JavaScript automatically calls the toString() method....
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › JSON › parse
JSON.parse() - JavaScript | MDN
The JSON.parse() static method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.
freeCodeCamp
forum.freecodecamp.org › javascript
Understanding strings as objects - JavaScript - The freeCodeCamp Forum
September 5, 2018 - In the following code, I initialize a string. To see what’s inside, I use Object.getOwnPropertyNames() and Object.values(). var str = 'hi'; console.log(Object.getOwnPropertyNames(str)); console.log(Object.values(str)); ...
TutorialsPoint
tutorialspoint.com › home › javascript › javascript strings object
JavaScript Strings Object
September 1, 2008 - The string is a sequence of characters containing 0 or more characters. For example, 'Hello' is a string. JavaScript strings can be created as objects using the String() constructor or as primitives using string literals.
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-convert-string-to-array-of-objects-javascript
How to Convert String to Array of Objects JavaScript ? - GeeksforGeeks
August 5, 2025 - Another approach to convert a string ... is to use custom delimiters and object construction. This method is particularly useful when the string format doesn't follow a strict JSON structure but contains well-defined patterns or delimiters. By splitting the string based on these custom delimiters, we can construct objects and create the desired array. Example: The following ...
Top answer 1 of 16
1667
I would recommend using JSON.stringify, which converts the set of the variables in the object to a JSON string.
var obj = {
name: 'myObj'
};
JSON.stringify(obj);
Most modern browsers support this method natively, but for those that don't, you can include a JS version.
2 of 16
182
Use javascript String() function
String(yourobject); //returns [object Object]
or stringify()
JSON.stringify(yourobject)
O'Reilly
oreilly.com › library › view › learning-javascript › 0596527462 › ch04s03.html
The String Object - Learning JavaScript [Book]
October 17, 2006 - The String object is probably the ... built-in JavaScript objects. A new String object can be explicitly created using the new String constructor, passing the literal string as a parameter: ... The String object has several methods, some associated with working with HTML, and several not. One of the non-HTML-specific methods, concat, takes two strings and returns a result with the second string concatenated onto the first. Example 4-2 demonstrates how to create a String ...
Author Shelley Powers
Published 2006
Pages 352
Programiz
programiz.com › javascript › examples › convert-object-string
JavaScript Program to Convert Objects to Strings
// program to convert an object to a string const person = { name: 'Jack', age: 27 } const result1 = String(person); const result2 = String(person['name']); console.log(result1); console.log(result2); console.log(typeof result1); ... In the above example, the String() function converts the ...
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › JSON › stringify
JSON.stringify() - JavaScript | MDN
The JSON.stringify() static method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.
SamanthaMing
samanthaming.com › tidbits › 62-5-ways-to-convert-value-to-string
5 Ways to Convert a Value to String in JavaScript | SamanthaMing.com
There's very little practical use for String objects as created by new String("foo"). The only advantage a String object has over a primitive string value is that as an object it can store properties: var str = "foo"; str.prop = "bar"; alert(str.prop); // undefined var str = new String("foo"); str.prop = "bar"; alert(str.prop); // "bar" StackOverflow: What's the point of new String(“x”) in JavaScript? @MaxStalker: I would use a different method depending on the application: "" + val: simply cast number to string - let's say inside of the .map() JSON.stringify(val): need to convert small non-nested object ·
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String
String - JavaScript | MDN
String primitives and String objects also give different results when using eval(). Primitives passed to eval are treated as source code; String objects are treated as all other objects are, by returning the object. For example:
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-convert-an-object-to-string-using-javascript
How to convert an object to string using JavaScript ? - GeeksforGeeks
July 11, 2025 - JSON.stringify() converts the JavaScript object to a string which is needed to send data over the web server. ... Example: In this example, we will use JSON.stringify() method for conversion of an object to a string.
W3Schools
w3schools.com › js › js_json_parse.asp
W3Schools.com
A common use of JSON is to exchange data to/from a web server. When receiving data from a web server, the data is always a string. Parse the data with JSON.parse(), and the data becomes a JavaScript object.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › toString
String.prototype.toString() - JavaScript | MDN
const stringObj = new String("foo"); console.log(stringObj); // Expected output: String { "foo" } console.log(stringObj.toString()); // Expected output: "foo" ... A string representing the specified string value. The String object overrides the toString method of Object; it does not inherit ...