15,995,827 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View .NET questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
Comments by Member 13814166 (Top 3 by date)
Member 13814166
9-May-18 20:13pm
View
I'm having trouble understanding how i in "function() { return i; };" is always 10. I thought only the declaration of i would be hoisted up, not the value it contained, am I incorrect in thinking this, or is there something I'm missing?
I would have assumed, if the output was the same, it would be written like this with lets rather than vars:
let i; //The declaration is hoisted up
function constfuncs() {
var funcs = [];
for(i = 0; i < 10; i++)
funcs[i] = function() { return i; };
return funcs;
}
var funcs = constfuncs();
console.log(funcs[5]()); //Outputs 5
Member 13814166
6-May-18 13:33pm
View
Deleted
Sorry for my stupidity, but I still do not understand. When I do this:
var t=0;
var a=t;
t+=1;
var b=t;
a is 0, and b is 1. Why are they not the same?
And thank you so much for sparing your time to help me :D
Member 13814166
6-May-18 13:24pm
View
Ok, I understand, but why, if instead of let i=0 the for loop created var i=0, does the function print "10" ten times? I would expect the items of the funcs array to have the same values as when the for loop used let i=0?