JS

CONVERT OBJECT TO STRING IN JAVASCRIPT

THE OBJECT var obj = {   email : "john@doe.com",   address : "123 Doe Street" };

JSON ENCODE

01

JSON ENCODE TO STRING var str = JSON.stringify(obj);

MANUAL FOR LOOP var str = ""; for (let i in obj) {   str += i + ":" + obj[i] + ", "; } str = str.substring(0, str.length -2);

MANUAL CONVERT

02

THE OBJECT var obj = {   email : "john@doe.com",   address : "123 Doe Street" };

CUSTOM FUNCTION

03

var obj = {   email : "john@doe.com",   address : "123 Doe Street"

  stringify : function () {     let s = "email: " + this.email + ", ";     s += "address: " + this.address;     return s;   } };

CLASS FUNCTION (A)

04

PERSON CLASS class person {   constructor (email, address) {     this.email = email;     this.address = address;   }   stringify () {     let s = "Email: " + this.email;     s += "Address: " + this.address;     return s;   } }

CLASS FUNCTION (B)

05

CREATE OBJECT + TO STRING var obj = new person(   "jon@doe.com", "123 Doe Street" ); var str = obj.stringify();