4

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]);

this is the console image

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]);
5
  • 3
    Is global === window? Commented May 17, 2018 at 18:00
  • people stop downvoting questions just because people are new to programming! Commented May 17, 2018 at 18:02
  • 3
    @Luke Please stop assuming the reasons people downvote. Commented May 17, 2018 at 18:04
  • Not is necessary use "global" 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. Commented May 17, 2018 at 18:06

4 Answers 4

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]);

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the explanation. I had changed to declare as var, however I am still getting the same problem, the output is still undefined...
@Emilylaw In that case, please copy the code you tried just now and add it as an Edit to your question!
@Emilylaw The updated code in your question seems to work just fine now: jsfiddle.net/cngv0fqp
2

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 = []

2 Comments

i would not suggest to use a global variable like "window". as you read, he is new to programming. i think it would be good to improve his thinking
I didn't "suggest" anything – just stated "global" is the issue.
2

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]);

Comments

1

A variable declared outside a function, becomes 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]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.