Convert Objects to Strings – JavaScript Solution

This is a preview lesson
Register or sign in to take this lesson.

Object to String

This tutorial show a snippet for converting a JavaScript object to string

Steps in converting a JavaScript object to string

For achieving this, we use the JSON.stringify() method to convert a JavaScript object to its string counterpart.

JavaScript Syntax

let obj = {
    id: 1,
    message: "Hello World!"
}

const stringFromObject = JSON.stringify(obj);

console.log(stringFromObject);
console.log(typeof stringFromObject);

/*
 * Output:
     {"id":10,"message":"hello"}
     string
*/