admin管理员组

文章数量:1401431

I am still very new to programming and javascript. The problem I am facing now is I am not able to access the data inside array. Here is my code snippet

global.arr = [];
var id = 12;

for (var i=0; i<5; i++) {
   arr.push(id);
   id++;
}
console.log(arr);
console.log(arr[0]);

My question is that, how can I access into the data and what did i do wrong here?


Here is the code I currently have, it still doesn't seem to work:

var arr = [];
var id = 12;

for (var i=0; i<5; i++) {
   arr.push(id);
   id++;
}
console.log(arr);
console.log(arr[0]);

I am still very new to programming and javascript. The problem I am facing now is I am not able to access the data inside array. Here is my code snippet

global.arr = [];
var id = 12;

for (var i=0; i<5; i++) {
   arr.push(id);
   id++;
}
console.log(arr);
console.log(arr[0]);

My question is that, how can I access into the data and what did i do wrong here?


Here is the code I currently have, it still doesn't seem to work:

var arr = [];
var id = 12;

for (var i=0; i<5; i++) {
   arr.push(id);
   id++;
}
console.log(arr);
console.log(arr[0]);
Share Improve this question edited May 17, 2018 at 18:27 Luca Kiebel 10.1k7 gold badges32 silver badges46 bronze badges asked May 17, 2018 at 17:57 EmilylawEmilylaw 615 bronze badges 5
  • 3 Is global === window? – castletheperson Commented May 17, 2018 at 18:00
  • people stop downvoting questions just because people are new to programming! – Luke Commented May 17, 2018 at 18:02
  • 3 @Luke Please stop assuming the reasons people downvote. – castletheperson Commented May 17, 2018 at 18:04
  • Not is necessary use "global" – Marcos Riveros Commented May 17, 2018 at 18:04
  • 1 @Luke No one is down voting just because people are new to programming, they down vote bad questions, no matter if you have a rep. of 1 or 100000. – Asons Commented May 17, 2018 at 18:06
Add a ment  | 

4 Answers 4

Reset to default 7

Edit: Expanding this a little more:

global is not a JavaScript object. There are global objects, but you access them through window not global. Your code would only work if you had, somewhere else, set global equal to window.

Unless you have a really good reason to use global (or window for that matter), just define the array (and other variables) with var or let.

let arr = [];
let id = 12;

for (let i=0; i<5; i++) {
   arr.push(id);
   id++;
}
console.log(arr);
console.log(arr[0]);

I believe global is the root of your issue. It works when in the browser and using window

window.arr = [];
var id = 12;

for (var i=0; i<5; i++) {
   arr.push(id);
   id++;
}
console.log(arr);
console.log(arr[0]);

However, it is better to not set directly to the window and instead use var arr = [] or let arr = []

To a global array you don't need to set

global.arr = [];

Just set a let variable:

let arr = [];
var id = 12;

See: Let

Then use your code:

let arr = [];
var id = 12;

for (var i=0; i<5; i++) {
 arr.push(id);
 id++;
}
console.log(arr);
console.log(arr[0]);

A variable declared outside a function, bees GLOBAL. you can declare the variable arr.

var arr = [];
var id = 12;
for (var i=0; i<5; i++) {
   arr.push(id);
   id++;
}
console.log(arr);
console.log(arr[0]);

本文标签: Unable to access array data in JavascriptStack Overflow