. for (var i = 0; i < 5; i++) {
// Will execute 5 times
}
for (let value of array) {
// do something with value
}
for (let property in object) {
// do something with object property
}
for (var i = 0; i < a.length; i++) {
// Do something with a[i]
}
. JavaScript objects are the same as Dictionaries in Python and Associative arrays in PHP.
. A scope in JavaScript defines what variables you have access to. There are two kinds of scope – global scope and local scope (function scope and block scope).
. Always declare local variables, not global variables (prevent name collisions and make debug easier).
. Functions, when declared with a function declaration, are always hoisted to the top of the current scope.
. Declare your functions before you use them.
. Lexical scoping – when a function is defined in another function, the inner function has access to the outer function’s variables.
. Whenever you create a function within another function, you have created a closure. Since closures have access to the variables in the outer function, they are usually used for two things:
1. To control side effects (ex. of side effects – Ajax request, a timeout or even a console.log)
2. To create private variables
. To know more about closures – https://stackoverflow.com/q/111102/5675325
. «Let’s imagine the next situation: a driver is sitting in a car. That car is inside a plane. Plane is in the airport. The ability of driver to access things outside his car, but inside the plane, even if that plane leaves an airport, is a closure. That’s it.», in: https://stackoverflow.com/a/16959645/5675325
. Debugging scopes with DevTools – use debugger or click in the line number in Sources tab
. A Promise is an object representing the eventual completion or failure of an asynchronous operation. Essentially, a promise is a returned object to which you attach callbacks, instead of passing callbacks into a function.
. Check all of these JavaScript questions to refresh your knowledge – https://github.com/tiago-peres/javascript-questions
References:
. https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
. https://css-tricks.com/javascript-scope-closures/
. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises