JavaScript Object vs JSON: What You Need to Know
Hey Geek !!
This blog is specially dedicated to people who are really confused about the difference between JSON and JS objects because they look like very similar syntax. So we can discuss and understand the difference between both of them.
What is JavaScript Object Notation (JSON)
JavaScript Object Notation (JSON) is written JavaScript, which is a text data format introduced by Douglas Crockford in 2021. It uses to transfer the data between server and browser/client applications. Earlier we used XML (Extensible Markup Language) for data transfers but it required more coding and has a larger size making it slow to process and transfer the data
Features of JSON
- Syntax is Straightforward
- Easy to learn and understand
- Language-Independent – which means that we can use all major programming languages like C, C++, C#, JS, Python, and many others and they know how to handle and manage JSON data.
How JSON look like
JSON object appear like key-value pairs, Keys are on the left side ( array, Boolean, color) and values on the right side, similar to JavaScript object but the difference is that keys are in JSON must be wrapped in double-quotes. JSON supports six data types, Array, Boolean, null, number, Object, and String. Also, we cannot add comments inside the JSON
{
"array": [
1,
2,
3
],
"boolean": true,
"color": "gold",
"null": null,
"number": 123,
"object": {
"a": "b",
"c": "d"
},
"string": "Hello World"
}
So, What about JavaScript object
Warning: If you already know about Object in other programming languages like Java, C++, C#, and more then please forgot it at this point in time because In JavaScript concept of objects are a little bit different. If you want to contract an object in other programming languages then you should create a class for that but in JavaScript, you don’t need to create classes.
In JavaScript, there are two type of data types of data type for holding types of values
- Primitive values
- Non-primitive values
Primitive values in JavaScript are strings, Booleans, null, undefined and number which are immutable data types means that once it gets created there is no way to change the value of a primitive data type
Non- Primitives are object references and mutable data types, JavaScript Object is a none-primitive data type, it helps you store different collections of data and it can be created by object literals, constructors, and prototypes.
The object is a key-value pair and that keys are not need to be wrapped with double quotes, also contain a function as of its member
How Object looks like
JavaScript supports Boolean, string, Null, Symbol, Object, Undefined, BigInt and Number
const person = {
name: "sam",
age: 12,
isActive: true,
address: " A29 xyz Street"
city: "consmic",
state: "CA",
getFullAdress: function() {
return this.address + this.city + this.state
}
}
Conclusion
JSON and JavaScript objects are having similar syntax but both are used for different purposes. In JSON keys are wrapped with double quotes.JS object supports all data type which is defined in JavaScript but JSON doesn’t support undefined data type
That’s all geek,! If I missed anything else please let me know in comment section
0 Comments