It amazes me how JavaScript can process arrays without encountering any errors

const vegetables = ["carrot", "tomato", "lettuce"];

vegetables.price = 500;

console.log(vegetables);          // (3) ["carrot", "tomato", "lettuce"]
console.log(veggies.price);       // 500

I'm intrigued as to why the line vegetables.price = 500; doesn't throw an error. Can someone please explain this in simple terms? Thank you for taking the time to read this. Any assistance would be greatly appreciated.

Answer №1

JS arrays behave like objects because they are essentially objects. This means you can perform all the typical object operations on arrays, such as adding and removing properties:

const a = [1,2,3];
a.prop = 'test';
delete a[1];
console.log(a);       // [1, undefined, 3]
console.log(a.prop);  // 'test'

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Use the Facebook API to easily access any user's profile picture by providing their unique Facebook user ID

For my JavaScript function, I am taking the input "user_id" and expecting it to provide me with the profile photo data. Everything works fine when a logged in user's profile picture is being displayed. However, when I pass a logged out user_id, an err ...

Encountering an error: Module missing after implementing state syntax

My browser console is showing the error message: Uncaught Error: Cannot find module "./components/search_bar" As I dive into learning ReactJS and attempt to create a basic component, this error pops up. It appears after using the state syntax within my ...

Transferring information from a template to a view within Django

I am currently in the process of creating a bus reservation platform using Django. When a user searches for buses on a specific route, a list of available buses is displayed. Each bus in the list has a 'book' button that redirects to a new page c ...

I am having trouble with the popstate function in React - it doesn't seem to be working. I am unsure how to trigger the browser backtab click properly. When I click

What is the best way to trigger a logout function in React when the browser back tab is clicked? I am looking for a solution where, upon clicking the back tab, a modal confirmation dialog appears with an "OK" button to log out of the application. const ha ...

Changing the 'badge' to 'panel' within the UI framework of 'ant design' has been set

Can the Badge be placed next to 'Info' in a Panel using ant design? https://i.sstatic.net/Lldc7.png View Code <div> <Collapse> <Panel header="Info" key="1"> <Badge count={4} style={{ b ...

The Redux store has been modified, yet the changes are not reflected in the

In my Posts.js component, I am mapping every object in the posts array. Within this function, I attempt to filter all notes that have the same post_id as the id of the current mapped post object and store them in a variable called filteredNotes. I then pas ...

The Angular material datepicker is not accurately capturing the date I am trying to select

I am facing an issue with the Angular Material datepicker where it does not select the date I choose. Instead, in my AngularJS controller, I always get the sysdate even if I select another date. Can anyone help me figure out what I am doing wrong? Here is ...

A div positioned in front of a centrally-located div

Managing a website with numerous headlines can be a challenge. When a user clicks on a headline, a button located 10px to the left of the headline should appear. However, the catch is that the headlines must always remain centered, regardless of whether th ...

Listener document.addEventListener (function() {perform the function as shown in the illustration})

Currently facing an issue that I'm struggling to resolve, attempting to execute a code snippet from the ngcordova website without success. While using the plugin $cordovaInAppBrowser.open ({... it generally functions well until the inclusion of the ...

Error messages cannot be custom in production when reading a 409 response JSON from the server

After setting up my asp.net core MVC server, I decided to implement better error handling. However, upon deploying the changes to the production environment, I noticed a discrepancy in how my server responds to 4xx errors. While everything works fine on m ...

Mastering the art of filtering JSON data with multiple conditions using .filter() and .includes() in JavaScript and React

Forgive me if this is a straightforward question, as I am new to the world of programming. My current project involves creating a web application that filters quotes based on specific keywords. The quotes are stored in JSON format, here's a glimpse at ...

Personalized modify and remove elements on a row of the DataGrid material-ui version 5 component when hovered over

In my React Js app, I am utilizing Material UI components or MUI v5 as the UI library for my project. Within the DataGrid/DataGridPro component, I am implementing a custom edit and delete row feature. The requirement is to display edit and delete icons w ...

What could be causing the "Cannot POST /api/" error to occur when attempting to submit a form?

Having issues with my basic website and struggling to find a solution. As a complete beginner in this field, I am stuck and need some guidance. Accessing http://localhost:3000/class/create works perfectly fine when running the server. However, trying to a ...

Performing a MySQL query to select multiple rows and fields with the AND operator simultaneously

I've got a table: + --------------------------+---------------------------+--------------+--------+ | ID | SKU_ID | DKEY | DVAL | + --------------------------+---------------------------+----------- ...

When JavaScript evaluates special characters in HTML, it interrupts the function call within an AJAX response

Recently, I have been developing a custom form completion feature for a website/tool that I am working on. After successfully implementing the search functionality, which displays results below the input field, I wanted to enable users to select a result ...

Is there a solution for converting arrays with duplicate keys to XML?

It is known that having duplicate keys in an array is not possible. The scenario here involves sending data to an API in XML format. Before converting the data to XML and sending it to the API, it needs to be held in an Array. The API only accepts the spec ...

Modify the array dynamically within the Factory

I am currently working on a simple app where I want to display embed videos with the click of a button. It was quite challenging for me to dynamically bind my embed players, but I managed to do it successfully. I created a factory that contains my data in ...

Angular 2: Changing HTTP Requests

I am trying to include a single parameter in all my web requests to disable caching forcibly. My goal is to append ?v=1535DC9D930 // Current timestamp in hex to the end of each request. I am coding this in plain ES5 JS, but the documentation is in Types ...

Guide to building a landscape in three.js

Creating a plane with numerous segments is the first step, but how do I go about adjusting the y position of the vertices afterwards? Appreciate your help in advance, ...

Turn off bodyparser when uploading files in Node.js

This query is quite similar to another one on Stack Overflow regarding how to disable Express BodyParser for file uploads in Node.js. The solution provided there was for Express3, but when tested with the updated Express 4, it didn't work as expected. ...