DEV Community

shunku
shunku

Posted on

1

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

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay