DEV Community

shunku
shunku

Posted on

Chapter 4: Objects and Arrays in JavaScript

Introduction to Objects

In JavaScript, an object is a standalone entity, with properties and type. It's an unordered collection of related data, of primitive or reference types, in the form of "key: value" pairs. These keys are also known as properties.

let person = {
  firstName: "John",
  lastName: "Doe",
  age: 30,
  country: "USA"
};

console.log(person.firstName); // Outputs: "John"
Enter fullscreen mode Exit fullscreen mode

You can access the properties of an object using dot notation (as above) or bracket notation (person["firstName"]). You can also modify the properties of an object:

person.age = 31;
console.log(person.age); // Outputs: 31
Enter fullscreen mode Exit fullscreen mode

Arrays and Array Methods

An array in JavaScript is a type of object used for storing multiple values in a single variable. Each value (also called an element) in an array has a numeric position, known as its index, and it may contain data of any data type—numbers, strings, booleans, functions, objects, and even other arrays.

let fruits = ["apple", "banana", "cherry"];
console.log(fruits[0]); // Outputs: "apple"
Enter fullscreen mode Exit fullscreen mode

JavaScript provides many functions that help you work with arrays. Here are some of the most common ones:

  • push: Add a new element to the end of an array.

    fruits.push("date");
    console.log(fruits); // Outputs: ["apple", "banana", "cherry", "date"]
    
  • pop: Remove the last element from an array.

    fruits.pop();
    console.log(fruits); // Outputs: ["apple", "banana", "cherry"]
    
  • shift: Remove the first element from an array.

    fruits.shift();
    console.log(fruits); // Outputs: ["banana", "cherry"]
    
  • unshift: Add a new element to the start of an array.

    fruits.unshift("apple");
    console.log(fruits); // Outputs: ["apple", "banana", "cherry"]
    

JSON (JavaScript Object Notation)

JSON is a format for structuring data that is used in JavaScript and many other programming languages. It's commonly used for transmitting data in web applications (e.g., sending data from a server to a client).

In JSON, data is presented in key/value pairs, just like JavaScript objects. Here's an example of JSON data:

{
  "employees": [
    { "firstName": "John", "lastName": "Doe" },
    { "firstName": "Anna", "lastName": "Smith" },
    { "firstName": "Peter", "lastName": "Jones" }
  ]
}
Enter fullscreen mode Exit fullscreen mode

In JavaScript, you can convert an object into JSON using JSON.stringify():

let person = {
  firstName: "John",
  lastName: "Doe"
};

let json = JSON.stringify(person);
console.log(json); // Outputs: '{"firstName":"John","lastName":"Doe"}'
Enter fullscreen mode Exit fullscreen mode

And you can convert JSON back into an object using JSON.parse():

let json = '{"firstName":"John","lastName":"Doe"}';
let person = JSON.parse(json);
console.log(person.firstName); // Outputs: "John"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)