Standard Built in Objects
Hello dear readers, today I am going to talk to you about two famous built in objects used frequently in JavaScript.
The first one is : JSON.stringify .
What is that exactly and how can you (dear reader) use it ?
Well let’s begin with the fisrt question : JSON.stringify is a built in function in JavaScript and it has one specific role, which is converting a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.
2/ How can you use it ?
JSON.stringify({ x: 5, y: 6 })
returns “{“x”:5,”y”:6}”.
So as you may have noticed stringify is transforming everything into a string ;) (duh)
Now let’s talk about JSON.parse :
The JSON.parse method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.
2/ How can you use it ?
‘{“result”:true, “count”:42}’
obj = JSON.parse(json);
So what does that mean exactly ? We had a string and we transformed it into a new Object ;)
Finally you can see the difference between these two methods , one of them(stringify) takes anything and turns it into a string , and the other(parse) takes a tsring a recostruct it into a new type of variables .
So here you go …….that was it for today, see you tomorrow.