JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – copy object with JSON stringify and parse method

JavaScript Quiz – copy object with JSON stringify and parse method

JSON stringify method –

JSON.stringify() method converts a JavaScript object or value to a JSON string. Sometimes, in order to save JSON object in DB. It’s required to convert it to a string.

It’s syntax look like –

JSON.stringify(value[, replacer[, space]])

For more detail about the javascript JSON stringify Method click here.

JSON parse method –

JSON.parse() method parse a JSON string and converts a JSON string to a javascript object. JSON.parse() works as a reverse of JSON.stringify().

It’s syntax look like –

JSON.parse(text[, reviver])

For more detail about the javascript JSON parse Method click here.

JavaScript Question

In this javascript quiz code, you need to find out does copy object with json stringify method and parse method remove reference of object.

First, we declare a detail variable using the const keyword and assign an object to it.

const detail = { name : “JS Startup” };

Now, create an another variable obj using const and in which we assign object variable with JSON parse and JSON stringify method.

In the next step, we change the name key value of obj variable to “JavaScript”.

obj[‘name’] = “JavaScript”;

Finally, we are checking the output of name key in detail variable using console log.

Any guess what’s the output ?

Answer – “JS Startup

Click To Run Code

Explanation

So, you know the answer is “JS Startup”. But how, doesn’t object pass reference when copying. Let me explain it you.

It’s true that object and array pass their reference to other variable instead of value.

But as you can see first we convert to object to string using JSON.stringy() method. Which returns a string not object.

JSON.stringify(object);

Now, the string we get is passed as a parameter to JSON.parse() method to again convert back it to javascript object.

But why we do all this. It’s simple that way we remove the object referencing. So, when we change the name key value of the obj variable. There is no effect on the detail variable.

At last, That’s why we get an answer “JS Startup“.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JS Quiz – can really instanceof operator work on string literal
  2. JavaScript Quiz – Function can be used as class using new operator
  3. JS Quiz – Reverse array element but in the same position
  4. JavaScript Quiz – filter array method based on the element size

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
0 Shares
Share via
Copy link