Examples to convert an object to string using JavaScript
1. Using String() Constructor
String() constructor converts the value of an object to a string.
Syntax:
String(object)
Example: Here, we will use the string() method to convert the objects to a string.
// Inputs
let bool_to_s1 = Boolean(0);
let bool_to_s2 = Boolean(1);
let num_to_s = 1234;
// Display type of first input
console.log( typeof(bool_to_s1));
// After converting to string
console.log( typeof(String(bool_to_s1)));
// Display type of first input
console.log( typeof(bool_to_s2));
// After converting to string
console.log(typeof(String(bool_to_s2)));
// Display type of first input
console.log( typeof(num_to_s));
// After converting to string
console.log(typeof(String(num_to_s)));
Output
boolean string boolean string number string
Explanation: This code demonstrates type conversion in JavaScript. It first creates a boolean value from 0 and 1, and then converts them to strings using the String()
constructor. Similarly, it converts a number to a string. Finally, it logs the types before and after conversion for each case.
2. Using JSON.stringify() Method
JSON.stringify() converts the JavaScript object to a string which is needed to send data over the web server.
Syntax:
JSON.stringify(obj)
Example: In this example, we will use JSON.stringify() method for conversion of an object to a string.
// Input object
let obj_to_str = {
name: "w3wiki",
city: "Noida",
contact: 2488
};
// Converion to string
let myJSON = JSON.stringify(obj_to_str);
// Display output
console.log(myJSON);
Output
{"name":"w3wiki","city":"Noida","contact":2488}
Explanation: This code converts the object obj_to_str
to a JSON string using JSON.stringify()
. It then logs the resulting JSON string to the console. This process serializes the object’s properties into a string representation suitable for data interchange.
Using plus (+) Operator with string
By default concatenation operation of a string with any data type value first, and convert the value to string type then concatenate it to string.
Syntax:
"" + object ;
Example: Here, we are using + operate to change objects to strings.
// Input objects
let obj1 = new Object();
let obj2 = { ww : 99 , ss : 22};
// Display type of objects before
// and after their conversion
console.log( typeof( obj1 ));
console.log( typeof( '' + obj1));
console.log( typeof( obj2 ));
console.log(typeof( '' + obj2 ));
Output
object string object string
Explanation: The code demonstrates converting objects to strings in JavaScript. It creates two objects, obj1
and obj2
, and displays their data types before and after conversion. By concatenating an empty string, the objects are converted to strings, changing their data types from object
to string
.
Using toString() method
Example:
// Input object
let obj = {
name: "w3wiki",
city: "Noida",
contact: 2488
};
// Conversion to string using toString() method
let objStr = obj.toString();
// Display output
console.log(objStr);
Output
[object Object]
Explanation: In this example, we use the toString() method directly on the object. By default, the toString() method returns “[object Object]”, which is a generic string representation of the object. This approach is straightforward but doesn’t provide detailed information about the object’s properties.
How to convert an object to string using JavaScript ?
To convert an object to string using JavaScript we can use the available methods like string constructor, concatenation operator etc.
Let’s first create a JavaScript object.
// Input object
let obj_to_str = {
name: "w3wiki",
city: "Noida",
contact: 2488
};