panaverse/learn-typescript

Difference between JSON object and Javascript object

HSM009 opened this issue · 1 comments

Please update the readme

Javascript Object
const detail = { "name": "Vipin", "age": 21 };

JSON Object
const detail = '{ "name": "Vipin", "age": 21 }';

JSON object is string. After parse they are separated into key/value.

In JavaScript, there's a distinction between a JavaScript object and a JSON object, although they may seem similar.

  1. JavaScript Object:

    const detail = { "name": "Vipin", "age": 21 };

    Here, detail is a JavaScript object. It's defined using object literal notation, denoted by curly braces {}. This object has two properties: "name" with the value "Vipin" and "age" with the value 21. This is a native JavaScript object.

  2. JSON Object:

    const detail = '{ "name": "Vipin", "age": 21 }';

    Here, detail appears to be a JSON-like string. JSON stands for JavaScript Object Notation, and it's a lightweight data-interchange format. The provided example, however, is a string literal, not a JSON object. It's important to note that JSON is primarily a data format used for exchanging data between a server and a client, and it's a subset of JavaScript object literal notation.

To use the JSON data, you need to parse it:

const detail = '{ "name": "Vipin", "age": 21 }';
const parsedDetail = JSON.parse(detail);

After parsing, parsedDetail becomes a JavaScript object equivalent to { "name": "Vipin", "age": 21 }.

In summary, while JavaScript objects and JSON objects share some similarities, they are not the same. JSON is a text format that is completely language-independent but uses conventions familiar to programmers of the C-family of languages, while JavaScript objects are native data structures in JavaScript itself.