How to Convert Objects to Strings in javascript?
To convert a objects to strings we can use JSON.stringify() or String()
functions. Here is an example follows.
// program to convert an object to a string
const emp = {
name: 'John',
age: 30
}
const empInfo = JSON.stringify(emp);
console.log(empInfo);
console.log(typeof emp);
Out put:
{"name":"John","age": 30}
string
// program to convert an object to a string
const emp = {
name: 'John',
age: 30
}
const empInfo1 = String(emp);
const empInfo2 = String(emp['name']);
console.log(empInfo1);
console.log(empInfo2);
console.log(typeof empInfo1);
Output:
[object Object]
Jack
string