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
🌐
W3Schools
w3schools.com › jsref › jsref_obj_string.asp
JavaScript String Reference
HTML Examples CSS Examples JavaScript Examples How To Examples SQL Examples Python Examples W3.CSS Examples Bootstrap Examples PHP Examples Java Examples XML Examples jQuery Examples · HTML Certificate CSS Certificate JavaScript Certificate Front End Certificate SQL Certificate Python Certificate PHP Certificate jQuery Certificate Java Certificate C++ Certificate C# Certificate XML Certificate ... W3Schools is optimized for learning and training.
🌐
W3Schools
w3schools.com › js › js_json_stringify.asp
JSON.stringify()
JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Bootcamp JS Certificate JS Reference ... A common use of JSON is to exchange data to/from a web server. When sending data to a web server, the data has to be a string. You can convert any JavaScript ...
🌐
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....
🌐
TutorialsPoint
tutorialspoint.com › home › javascript › javascript strings object
JavaScript Strings Object
September 1, 2008 - JavaScript strings can be created as objects using the String() constructor or as primitives using string literals. Use the following syntax to create a String object −
🌐
W3Schools Blog
w3schools.blog › home › javascript string object
Javascript String Object - W3schools
April 28, 2016 - A JavaScript String object represents a sequence of characters. 1. By using string literals. 2. By using String() constructor. When we create a string literal browser automatically converts it to a String object.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String
String - JavaScript | MDN
See "String primitives and String objects" below. String literals can be specified using single or double quotes, which are treated identically, or using the backtick character `. This last form specifies a template literal: with this form you can interpolate expressions. For more information on the syntax of string literals, see lexical grammar. There are two ways to access an individual character in a string.
Find elsewhere
🌐
W3Schools
w3schools.com › jsref › jsref_string.asp
JavaScript String() Method
String() is an ECMAScript1 (JavaScript 1997) feature. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
🌐
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.
🌐
W3Schools
w3schools.com › jsref › jsref_tostring_string.asp
JavaScript String toString() Method
The toString() method is used by JavaScript when an object needs to be displayed as a text (like in HTML), or when an object needs to be used as a string. ... If you want to use W3Schools services as an educational institution, team or enterprise, ...
🌐
O'Reilly
oreilly.com › library › view › learning-javascript › 0596527462 › ch04s03.html
The String Object - Learning JavaScript [Book]
October 17, 2006 - <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Exploring String</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <script type="text/javascript"> //<![CDATA[ var sObj = new String( ); var sTxt = sObj.concat("This is a ", "new string"); document.writeln(sTxt); //]]> </script> </body> </html> There is no known limit to the number of strings you can concatenate with the String concat method. However, I rarely use this myself; I prefer the String operators, such as the string concatenation operator (+). The properties and methods available with the String object are listed in Table 4-1.
Author   Shelley Powers
Published   2006
Pages   352
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Understanding strings as objects - JavaScript - The freeCodeCamp Forum
September 5, 2018 - I’m trying to better understand the internal structure of a string. 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)); ...
🌐
Favtutor
favtutor.com › articles › convert-string-to-object-javascript
Convert String to Object in JavaScript (with JSON.Parse)
December 11, 2023 - Learn how to convert string to object in JavaScript using JSON Parse method. We also explored how to handle Date and Functions in JSON.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Working_with_objects
Working with objects - JavaScript | MDN
For example, this example creates an object named myCar, with properties named make, model, and year, with their values set to "Ford", "Mustang", and 1969: ... Like JavaScript variables, property names are case sensitive. Property names can only be strings or Symbols — all keys are converted to strings unless they are Symbols.
🌐
W3Schools
w3schools.com › js › js_object_intro.asp
JavaScript Objects Learning Path
All JavaScript values, except primitives, are objects. A primitive data type is data type that can only store a single primitive value. JavaScript defines 7 types of primitive data types: ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
🌐
Programiz
programiz.com › javascript › examples › convert-object-string
JavaScript Program to Convert Objects to Strings
When using the String() function on an Object, the converted result will give [object Object].