Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8123007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:01:24+00:00 2026-06-06T06:01:24+00:00

var UserBoard = new Array(20,20); for(var i = 0; i < 21; ++i){ for(var

  • 0
var UserBoard = new Array(20,20);
for(var i = 0; i < 21; ++i){
    for(var j = 0; j < 21; ++j){
        UserBoard[i,j] = 0;
    }
}
document.write(UserBoard[3,5]);
UserBoard[4,5]=1;
document.write(UserBoard[3,5]);

http://jsfiddle.net/XbyqN/2/

it’s quite simple but I don’t know why does this. Alert should be 0, not 1 since I’ve initialized the 2d array to 0.

Can someone explain me why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:01:26+00:00Added an answer on June 6, 2026 at 6:01 am

    Let’s break it down

    var UserBoard = new Array(20,20);
    

    You are creating an array with two slots, both of them containing the value “20” (int). So your array is [20, 20]

    Next, your loop :

    for(var i = 0; i < 21; ++i){
      for(var j = 0; j < 21; ++j){
        UserBoard[i,j] = 0;
      }
    }
    

    Two dimensional arrays are not defined like this. In that case, only the “j” counter does something. The “i” is simply ignored. So you end up with an array as follow : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

    Next, the assignement :

    UserBoard[4,5]=1;
    

    Is equivalent to :

    UserBoard[5]=1;
    

    And your alert :

    alert("test: " + UserBoard[3,5]);
    

    Is equivalent to :

    alert("test: " + UserBoard[5]);
    

    That’s why you get “1” as alert.

    If you want two dimensional arrays, you should use the following notation :

    UserBoard[4][5] = 1;
    

    Read it all here on MDN : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

var chart; var d = new Array(); function click() { d = document.getElementById('graph:hi').value; alert(d);
var conversations = new Array(); jQuery('.CChatWindow').each(function(){ if (jQuery(this).is(:visible) && jQuery(this).attr(data-conversationid) != 0) { alert(jQuery(this).attr(data-conversationid));
var center = new google.maps.LatLng(18.133333, 21.566667); var map = new google.maps.Map(document.getElementById('map'), {zoom: 11,center: center,mapTypeId:
var longitudeArray = new Array(<?php $result = count($longitudeArray); if ($result > 1){ echo implode(',',
var script = document.createElement( 'script' ); script.type = 'text/javascript'; script.src = 'http://www.mydomain.com/bleh.php'; script.id =
var theNewWindow = window.open('http://www.example.com', 'example', ''); How can I use the document object of
var fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); using(var writer = new StreamWriter(fs)) writer.Write(....); If
var parser = new DOMParser(), d = parser.parseFromString('<?xml version=1.0?><div class=a>Hello</div>', 'application/xhtml+xml'); console.log(d.querySelector('*')); console.log(d.querySelector('div')); console.log(d.querySelector('div.a'));
var queueitem = context.CrawlerQueues. Select(cq => new{cq.Guid,cq.Result}). SingleOrDefault(cq => cq.Guid == guid); Is above
var REGEX = new RegExp((?=[hms])g) var TIME = _GET('t', '0').split(REGEX) var hours = TIME[TIME.indexOf('h')-1]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.