This question already has answers here:
Dynamic variable name in loop
(3 answers)
Closed 4 years ago.
If I have to create n variables a_1, a_2, a_3 ... a_n, where n is determined during runtime, how will I be able to do it?
Obviously, this code won't work:
var n = prompt("Enter number of variables?");
for (i=0; i<=n; i++) {
var a_i
}
As n is given by user it is not possible to pre-determine number of variables to be created.
In other words, Is it possible to create a variable with name from another variable in JS?
Yes, like this
for (i=0; i<=n; i++) {
window['a_'+i] = undefined;
}
Related
This question already has answers here:
How do JavaScript closures work?
(86 answers)
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 1 year ago.
I don't understand why the behavior of the function is funny.
let printNumTwo;
for (let i = 0; i < 3; i++) {
if (i === 2) {
printNumTwo = function() {
return i;
};
}
}
let i =5
console.log(printNumTwo());
I thought the function is created globally and it will have to return 5. Why the function has small scope? Where could i read about what more.
PS Sorry for my english and my probably stuped question. I need more understandable info for this TY
This question already has answers here:
JavaScript loop variable scope
(5 answers)
Closed 2 years ago.
Curious about why i becomes 5 in the end?
for (var i = 0; i < 5; i++) {
console.log('A: ', i);
}
console.log('B: ', i);
i++ increases the value of i at the end of each loop.
The loop goes round until the condition (that i is 4 or less) isn't true.
When i is 5, that is the first time the condition isn't true.
This question already has answers here:
What does i = (i, ++i, 1) + 1; do?
(7 answers)
Closed 4 years ago.
Could you tell me what this i means after the comma. I am struggling to understand this syntax or even find it in the internet.
let i;
this.questions = document.querySelectorAll('.question'), i;
i is not used anymore in the function
If setting a debugger after this lines. i is undefined
It doesn't seem to be of any use indeed.
As an information, it's just to declare / instantiate your variables. Take a look :
let i, j;
j = 1, i = 2;
console.log(i, j);
This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 4 years ago.
My Code (Javascript):
var num = 0;
function change(){
var num = 10;
}
change();
document.write(num);
The result should be 10 but it is showing 0. Why?
If the code is wrong, what is the correct way to do this?
You are setting another local variable called num.
Just change the inner var num to num.
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
For instance if I construct a new object type and create a few objects
function website(name,users)
{
this.name = name;
this.users = users;
}
var goog = new website("goog", "3,000,000");
var fireFox = new website("fireFox", "1,000,000");
var ie = new website("ie", "10");
And I push them into an array
var websites = [];
websites.push(goog,fireFox,ie);
Is there a way I can access a property of each object in the array through a loop?
For instance
for (var i=0;var<websites.length;i++)
{
console.log(websites[0.name]);
}
I know this code doesn't work but I hope it clarifies what I'm trying to ask.
Thanks!
When you say
websites[0.name]
It will try to get 0's name property, which is not valid. So, you should access it like this
websites[i].name
websites[i] will refer to the WebSite object in the array, at the index i and you are getting the name property with the . operator.
Also, your loop variable should be used in the for loop's condition, like this
for (var i=0; i < websites.length; i++)