. Advertisement .
..3..
. Advertisement .
..4..
There are plenty of ways to convert Object to String in JavaScript. Each of them has a distinct approach that you may need in different scenarios. Let’s check them out.
Convert Object To String In JavaScript
Using String()
The String()
constructor is a common way to create a String object. But you can also use it as a function, where it carries out type conversion. Keep in mind that while String()
returns a string primitive, the constructor with the new keyword returns an instance of the String class, which isn’t a primitive.
Here is how we can rewrite the example above with String()
:
const site = {
name: 'ITTutoria',
ranking: 1
}
const result1 = String(site);
const result2 = String(site['name']);
console.log(result1);
console.log(result2);
console.log(typeof result1);
Output:
[object Object]
ITTutoria
string
It is important to note how String()
converts an Object into a String. For the whole Object, the former produces [object Object], which is not particularly useful if we want to print its content. You will want to look at other options when that is your purpose.
Using JSON.stringify()
The method stringify()
from JSON can convert a JavaScript value or object into a JSON string. It can also replace values if you provide a replacer function or include only certain properties when you provide a replacer array.
Syntax:
JSON.stringify(value, replacer, space)
Parameters:
- value: this is the value you want to convert into a JSON string.
- replacer: this optional parameter can be a function or an array of numbers or strings. If it is a function, it will change the way the stringification process works. Meanwhile, an array will determine which properties of value the method stringify() should convert.
- When this parameter is null or omitted, the method will convert every property to the JSON string.
- space: A Number or String object used for readability purposes. The method will use it to format white space in the output string, such as characters, line breaks, or indentation.
Example:
const site = {
name: 'ITTutoria',
ranking: 1
}
const result = JSON.stringify(site);
console.log(result);
console.log(typeof site);
console.log(typeof result);
Output:
{"name":"ITTutoria","ranking":1}
object
string
In the snippet above, we create an Object containing a collection of data about a website. The method JSON.stringify()
helps us convert it to a String, which can be printed into the console with console.log()
. The type of the Object and String can be verified with the typeof operator.
Compared to String()
, JSON.stringify()
can create a string that contains the items inside the original Object. Most of the time, this is what we are looking for. And if you want to read JSON files with JavaScript, read this guide.
Using Object.entries()
The method entries()
returns an array of string-keyed property pairs of an Object. You can think of it like the for in loops in Python, which also go through each item in a sequence at a time.
Example:
const site = {
name: 'ITTutoria',
ranking: 1
}
for (let value of Object.values(site)) {
console.log(value)
}
Output:
ITTutoria
1
A for statement can be used to iterate over the array returned by the method values()
as above. This way, you can print each pair of the original Object into the console.
Conclusion
You can use String()
, JSON.stringify()
, or Object.entries()
to convert Object to String in JavaScript. Keep in mind that they don’t work in the same way or produce the same result.
Leave a comment