Javascript - Array vs JSON vs Object basics with Examples

Array, JSON, and Objects are valid objects used to interact and manipulate data in javascript applications.

This post talks about the difference between an array, JSON, and objects with examples. You can also check other posts on Javascript Add,Delete from Array

Javascript Object Basics examples

Objects are the main building blocks of Object-oriented programming concepts. Objects can be used to carry the data in applications. There are predefined objects as well as can be declared user-defined objects. Document and window are predefined objects in javascript.

Objects can contain properties and functions. the object can also be used with an instance or object name Objects can be created in many ways, One way is using new operatorand the other is using object literal syntax.

How to Declare, initialize the object using a new operator and constructor

Below is an example create an empty object using a new operator with an Object constructor.

new is a keyword and operator to create an instance of an object.

We can create an instance of the object using a new operator followed by a Constructor.

var firstObject = new Object();
console.log(firstObject); // {}

The above code creates an object and initializes it with an empty object.

How to create an object using Object literal syntax?

There are many ways we can create an object literal syntax.

An empty object can also be created and initialized with object literals.

Object literals are enclosed in curly braces with data

var firstObject = {};
console.log(firstObject); // {}

or

object can be created with object literals data
var obj={"kiran":"test"}

This is preferred the way of creating an object using literal syntax

How to assign properties to javascript objects?

javascript object contains properties. Each property can be a primitive type - Boolean, null, undefined, number, string, and symbol or any object

var firstObject = {};
firstObject.id = 1;
firstObject.name = "JOHN";
console.log(firstObject); // {id: 1, name: "JOHN"}

How to access object value using properties

Object data can be retrieved using the property name of an object.

property syntax.

object.propertyname;

Following is an example

var obj = { id: 1, name: "JOHN" };
console.log(obj.id); // 1
console.log(obj.name); // "JOHN"

How to get the size of an object in javascript

Object size is a count of properties in an object.

Following is an example of finding the keys of an object.

var obj = { id: 1, name: "JOHN" };
console.log(Object.keys(obj).length); // 2

how to check key exists in an object or Not?

the key can be checked in an object using the hasOwnProperty method

var obj = { id: 1, name: "JOHN" };
obj.hasOwnProperty("id"); //true;
obj.hasOwnProperty("id"); //false;

JSON Object examples in javascript

JSON is a javascript object notation used to transfer data between client and server.

It contains keys and values of javascript data types.

It is useful in many programming languages to interact with REST API JSON data containing key and value pairs. Here is a sample JSON object in javascript

{
"name": "Kiran",
"salary":2000
}

How to create and Initialize a JSON object

There are many ways we can create a JSON object in javascript. Empty Object Creation

var JsonObject = {};

or JSON object can be created using object

var jsonObject = new object();

JSON object always created using curly braces Initialize JSON object with key and values

var jsonObject = { name: "kiran", id: 1 };

The above object is created with the name and key-value pairs like a javascript object and each key and value pair are separated by a comma. Create JSON Object Arrays Arrays in javascript can be written using square brackets which contain a collection of objects

var jsonObject={"name":"kiran","id":1,depts:["dept1":"HR","dept":"admin"]}

The above example JSON object contains a depts array of two objects.

How to Convert JSON string to javascript object with examples

Json text can be converted to javascript object using JSON.parse()method. parse() method takes a json string and returns a Javascript object, if an invalid json string is passed, returns an empty object string

how to check key exists in an object or not a key can be existed in the object.

We can check key exists or not using the hasOwnProperty method

var jsonString = ‘{ "name":"kiran",salary:1000}’;
var javascriptObject = JSON.parse(jsonString);
console.log(javascriptObject.name)// kiran
console.log(javascriptObject.salary)// 1000

Convert JSON to String using stringify method

JSON.stringify() function is used in following cases convert json into String

var obj = { name: "kiran", salary: 2000 };
var json = JSON.stringify(obj);

arrays can also be stringified using Stringify method

var myarray = ["Ram", "Fran", "Sai"];
var myjson = JSON.stringify(myarray);

Array in javascript with examples

The array is a collection of elements with index order. Declare and initialize an array

let array = [1, 2, 3, 4, 5];

and an array of the object also declare and initialized using the below literal syntax

let array = [
  {
    id: 1,
    name: "john",
  },
];

Conclusion

In this tutorial, You learned different basic operations on Arrays, JSON, and Objects.