Expressions involving arrays in JavaScript

While diving into Eloquent JS, I stumbled upon this code snippet that left me puzzled. The explanation given didn't quite click with me, so I'm reaching out for some clarification on why the second console.log returns the value it does.

Take a look at the code below:

var listOfNumbers = [2, 3, 5, 7, 11];
console.log(listOfNumbers[1]);
// → 3
console.log(listOfNumbers[1 - 1]);
// → 2

Any thoughts on this?

Answer №1

2 stands as the initial value in the array starting at zero index

listOfNumbers[1 - 1] = listOfNumbers[0] = 2

Answer №2

The listNumber array holds the values 2, 3, 5, 7, and 11

listNumber[0] represents 2

listNumber[1] equals 3

listNumber[2] equals 5 listNumber[3] equals 7 listNumber[4] equals 11

listNumber[1-1] => listNumber[0] => 2

console.log(listNumber[0]); //will output 2 to the console

Answer №3

In programming, arrays are typically 0 indexed. This means that the first element in an array is accessed using index 0. For example, if we have a list called list, then list[0] would refer to the first element in the list which is 2, and list[1] would refer to the second element which is 3.

Answer №4

The initial element in an array is referred to as [0], followed by the second one, [1]. Remember, array indexes always commence at 0.

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

How can users create on-click buttons to activate zoom in and zoom out features in a Plotly chart?

I am currently working on an Angular application where I need to implement zoom in and zoom out functionality for a Plotly chart. While the default hoverable mode bar provides this feature, it is not suitable for our specific use case. We require user-cr ...

What is the best way to send variables from JavaScript to PHP while utilizing Ajax technology?

While I have noticed similar questions like this one before, I want to address my specific concerns. In previous examples, the questioner referred to using Ajax in a format similar to this: $.ajax({ type: "POST", url: 'logtime.php', ...

"Encountering a connectivity problem with Apollo server's GraphQL

I am facing a networking issue or possibly a bug in GraphQL(Apollo-Server-Express). I have tried various solutions but to no avail. When sending a query, everything seems perfect - values checked and request showing correct content with a 200 status code. ...

Apply a custom CSS class to every `tr` element that includes a particular value

I'm attempting to assign a class to a tr element (last_value_row) in a form when the target value in the first td of the last_value_row element is "New Target". Currently, only the first element will have the class added, while all other elements with ...

I need assistance in locating an error; the error message states that $ is not defined and an object is expected

Issue with $ not being defined, object expected.. I am trying to verify if all sets of radio buttons are checked when a button is clicked! Please help. <script type="text/javascript> $(document).on('click', 'form', function () { ...

Leveraging a tool to dynamically modify the styles of the active elements based on user interactions

My goal is to enhance the flexibility of my service by enabling it to handle any input field dynamically. Currently, I find myself manually coding everything, which is becoming quite labor-intensive. Is there a way to pass the element object when the eleme ...

Showing the elements of a python list in a dropdown menu on a webpage using AJAX

I'm currently utilizing Django and AJAX to create a chained dropdown feature. The user will initially choose a brand_name from a dropdown menu, and based on that selection, the names of all products made by that brand will be populated in a second dro ...

Strategies for breaking apart a large, monolithic Node.js JavaScript application

My node.js server application is expanding, and I am looking to split it into multiple files. Below is a snippet of the current monolithic server.js file: var express = require('express'); var app = express(); // other initialization code etc / ...

Fastest method to set a PHP variable to a value from an array that might be non-existent without triggering an error

This situation is similar to a game of code golf, but it's not off-topic because the issue frequently arises during my work. Keeping code concise and readable is definitely relevant. //$array = ['A' => 'FOO', 'B' => ...

Executing a function in ExpressJS at regular intervals of 24 hours

Can someone share the most effective method to schedule an automated task every 24 hours in ExpressJS? I have thoroughly researched possible solutions but haven't found any option other than using an infinite loop. Is there only one way to achieve th ...

Using Three.js to generate a CSS3DObject containing an HTML iframe from a different origin results in an error

The HTML code snippet below incorporates Three.js for creating a 3D scene and rendering it with CSS. It includes two separate HTML files: index.html and moo.html. <!doctype html> <html lang="en> <body> <script src="js/Three.js">&l ...

error: 1 exit status returned by the ld collector, reference undefined

New to the world of programming and facing a challenge. I'm stuck trying to compile this code, but keep encountering the following error: I suspect it may not be related to functions like insert_array_ascend, get_value, read_value, or is_in_array, bu ...

receiving an undefined value from a remote JSON object in Node.js

I have been struggling to extract the specific contents of a JSON api without success. Despite my best efforts, the console always returns undefined. My goal is to retrieve and display only the Question object from the JSON data. After spending several h ...

What is the best way to link two Mongoose models using their IDs for cross-referencing?

I need help with a scenario involving two models: project and user. projectSchema = { project_name: String, users: [user_id] } userSchema = { email: String, projects: [project_id] } Imagine a situation where a user wants to create a new project. I ...

Creating objects that are a fraction of another's width

My goal is to create 3 responsive divs that fill the width of the window and adjust their width based on the window dimensions. However, I'm facing an issue with JavaScript where it seems to be miscalculating the window width, causing the objects to o ...

Properly implementing prototypal inheritance: best practices

While there are numerous discussions on prototypal inheritance in JavaScript already, I assure you this is not just another lazy copy. Having thoroughly read all existing threads, I've noticed a plethora of syntactic approaches and varying answers whi ...

Webpack 4 Failing to Properly Render Select Tag

I am encountering an issue with a basic select element that is not displaying correctly due to Webpack. The screenshot below illustrates the final rendered page source, where the closing tag for the select element has been incorrectly positioned before the ...

What is the best way to implement seamless transitions between different components in my code?

I'm currently developing my own single page application and I want the content to have a smooth animation effect as I scroll down, instead of being static. Despite searching for guides on this topic, I haven't found any helpful resources yet. An ...

The timestamp will display a different date and time on the local system if it is generated using Go on AWS

My angular application is connected to a REST API built with golang. I have implemented a todo list feature where users can create todos for weekly or monthly tasks. When creating a todo, I use JavaScript to generate the first timestamp and submit it to th ...

Using iFrames to switch sources repeatedly

I have created an iFrame and I am looking to dynamically change its source to one of 10 different URLs each time it loads: Here is the desired behavior: iFrame loads => change to example.com => iFrame loads => change to example1.com => iFrame ...