String array in JavaScript

I'm puzzled by a strange issue I've come across. Here's what's happening: I declare an array called status=new Array(). Then, I loop through from 0 to N-1, and assign status[i]="idle";. When I try to check the values using alert, they all show up as a comma character ,. Can anyone help me figure out what's causing this unexpected behavior?

var status=new Array();
window.onload = function() {
    for(var i=0;i<5;i++) {
        status[i]="idle";
        alert(status[i]);
    }
}

Answer №1

Consider using a different variable name instead of global variables. It seems that there might be an issue with shadowing the existing window.status property (I'm surprised by this; perhaps the array is being converted to a string upon assignment). When declaring a variable at global scope, it becomes a property of the window object, hence why window.status is affected.

This demo (source) highlights the issue in Chrome, while this demo (source) uses a different variable name and functions correctly.

It's worth noting that this behavior is browser-dependent. Firefox allows redefinition of window.status, unlike Chrome which restricts it.

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

Creating a dynamic user interface with multiple tab navigations using jQuery on a single web

On my current HTML page, I am facing an issue with multiple tab navigations. When I click on one navigation, it also affects the other tab navigations. I cannot seem to find a way to only affect the tab navigation that I am clicking on without hiding the ...

display in a modal-body and jdata

Within my MySQL database, there are several columns stored, including: headline, description, place, resume Currently, I am able to display the headline and description in a modalbox using the following code. However, I now wish to also showcase the pl ...

Unexpected data type being returned by Swift .map function

I am attempting to convert two different arrays of doubles into an array of MKMapPoints. These arrays, x and y, are nested within another array that contains various data types like Strings and booleans. Here is the model: struct HostModel: Identifiable, ...

Unable to redirect with Asp response.redirect

I have a Login popup form where I use an ajax post request to Login.asp script in order to prevent the page from going to the POST URL after submission. <script> $(function() { $('#contactForm').submit(function(e){ e.preventDe ...

Assistance with Javascript Objects Using JSON and AJAX

Currently, I am utilizing Ajax to retrieve data from my Json file. A problem I am facing is that in one particular div of my html, I need to include both a heading and a paragraph. I attempted to create a property like so: "headingpara": "<h1> blah ...

React and Rails are not playing nice when it comes to AJAX POST requests - all

I'm currently facing an issue while setting up this AJAX POST request in my react component to interact with my rails api controller. The browser console shows a 404 error and I am unable to trigger the pry debugger. react/src/pages/HomeIndex.js ge ...

Is it possible to efficiently iterate through map keys in axios using Vue.js?

Currently, I am working on Vue.js exercises that I have created. I am successfully populating a table with data from a JSON API, but I have encountered a challenge. The API contains multiple keys with similar names (strIngredient1, strIngredient2, strIngre ...

Discovering similar sets of strings in R

My data consists of a vector with approximately 8000 strings, where each string represents the name of a company. Objective The goal is to group these company names into clusters based on similarity. For example, companies like ROYAL DUTCH SHELL, SHELL U ...

React: Dynamically update text input based on selected option

After selecting an option, I want to display a new text input. However, the old value entered remains on the screen even when I change the selection. How can I improve this functionality? Any suggestions would be appreciated. class loadComponent extends ...

Stop span elements from being removed within a `contenteditable` container

I am facing a challenge with an editable div that contains a span element which I would like to prevent users from deleting. My development environment is Vue3. Currently, if the user presses backspace while their cursor is to the right of the span or sel ...

Trying out an alert using HtmlUnit

I have grasped the concept somewhat from the instance of "Watching for 'alerts'" on this website: HtmlUnit- Javascript Tutorial. Being new to .NET, the functionality of Collections.singletonList is unclear to me, and after some research, I discov ...

Explaining the process of supplying an event to the setState function

I'm struggling with the following code snippet: h(e) { console.log(e.target.value); this.setState(state => { return {editData: e.target.value}; }); } Unfortunately, it seems like it's not working as e ...

Is it necessary to download all npm packages when starting a new project?

I recently started learning about npm packages and node. I noticed that when installing packages, they create numerous folders in the "node modules" directory. This got me thinking - when starting a new project, do I need to re-install all these packages ...

React: Ever wonder why changing the state directly ends up putting the component in such a peculiar state?

Before we proceed, I want to clarify that I am aware that mutating the state is not a recommended practice. However, I am curious to explore the potential consequences of directly mutating the state. Let's consider a simple todo app that stores todo ...

Tips for fetching form data transmitted via HTTPS in Node.js?

As someone new to back-end security, I'm hoping for some guidance without judgement: When receiving values over HTTP in my node application, the form data is easily accessible in the request object using req.body.{name of input element} However, whe ...

Using jQuery to dynamically insert a table row with JSON data into dropdown menus

I'm working on a web form that includes a table where users can add rows. The first row of the table has dependent dropdowns populated with JSON data from an external file. Check out the code snippet below: // Function to add a new row $(function(){ ...

What is the best way to keep a header row in place while scrolling?

I am looking to keep the "top" row of the header fixed or stuck during page scrolling, while excluding the middle and bottom rows. I have already created a separate class for the top row in my header code: view image description ...

What is the proper way to invoke the function located within a jQuery plugin?

I have incorporated a jquery plugin called vTicker on my webpage for automatic vertical news scrolling. This plugin, available from this link, works seamlessly with an rss jquery plugin. The integration is successful, but I now have a requirement to add a ...

Issues with Jquery/AJAX function not responding to onChange Event

I am currently working on a project where I need to populate a dropdown menu based on the value selected from another dropdown. However, I am encountering an issue with the function that I am trying to call in the onChange event of the select tag. This is ...

"Selecting an element through Drag/Drop will result in the element being

INQUIRY I've encountered an issue with a draggable element ($("#draggable")) that interacts with a checkbox ($('#checkable')). When the $('#draggable') is dragged onto a box ($('#droppable')), it checks $(&apos ...