You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>Within the function, we first declare the <b>name</b> variable with the <b>var</b> keyword. This means that the variable gets hoisted (memory space is set up during the creation phase) with the default value of <b>undefined</b>, until we actually get to the line where we define the variable. We haven't defined the variable yet on the line where we try to log the <b>name</b> variable, so it still holds the value of <b>undefined</b>.</p>
<p><code>[1, 2, 3, 4, 5].map(console.log);</code> is equivalent to <code>[1, 2, 3, 4, 5].map((val, index, array) => console.log(val, index, array));</code></p>
@@ -85,24 +53,8 @@ export default [
85
53
console.log(shape.diameter());
86
54
console.log(shape.perimeter());
87
55
`,
88
-
selections: [
89
-
{
90
-
des: '<code>20</code> and <code>62.83185307179586</code>',
91
-
correct: 0,
92
-
},
93
-
{
94
-
des: '<code>20</code> and <code>NaN</code>',
95
-
correct: 1,
96
-
},
97
-
{
98
-
des: '<code>20</code> and <code>63</code>',
99
-
correct: 0,
100
-
},
101
-
{
102
-
des: '<code>NaN</code> and <code>63</code>',
103
-
correct: 0,
104
-
},
105
-
],
56
+
selections: ['<code>20</code> and <code>62.83185307179586</code>','<code>20</code> and <code>NaN</code>','<code>20</code> and <code>63</code>','<code>NaN</code> and <code>63</code>'],
57
+
correct: 1,
106
58
explanation: {
107
59
html: `
108
60
<p>Note that the value of <code>diameter</code> is a regular function, whereas the value of <code>perimeter</code> is an arrow function.</p>
<p>Because of the event queue in JavaScript, the <code>setTimeout</code> callback function is called <em>after</em> the loop has been executed. Since the variable <code>i</code> in the first loop was declared using the <code>var</code> keyword, this value was global. During the loop, we incremented the value of <code>i</code> by <code>1</code> each time, using the unary operator <code>++</code>. By the time the <code>setTimeout</code> callback function was invoked, <code>i</code> was equal to <code>3</code> in the first example.</p>
<p>The variables declared with var keywords are <a href="https://developer.mozilla.org/en-US/docs/Glossary/Hoisting" rel="noopener noreferrer">hoisted</a> in JavaScript and are assigned a value of <em>undefined</em> in the memory. But initialization happens exactly where you typed them in your code. Also, <em>var-declared</em> variables are <a href="http://2ality.com/2011/02/javascript-variable-scoping-and-its.html" rel="noopener noreferrer">function-scoped</a>, whereas <em>let</em> and <strong>const</strong> have block-scoped. So, this is how the process will look like:</p>
0 commit comments