411 REFERENCE · Wikibooks
JavaScript/Variables/Exercises
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Topic: Declaration and Initialization
{Which line(s) lead to an error?
/* 1 */ "use strict";
/* 2 */ let a = 0;
/* 3 */ a = 1;
/* 4 */ const a = 10;
/* 5 */ alert(a);
|type="[]"}
- Line 2
- Line 3
+ Line 4
- Line 5
- None of the above
{Which line(s) lead to an error?
/* 1 */ "use strict";
/* 2 */ let a = 0;
/* 3 */ a = 1;
/* 4 */ const b = 10;
/* 5 */ b = 100;
|type="[]"}
- Line 2
- Line 3
- Line 4
+ Line 5
- None of the above
{Which line(s) lead to an error?
/* 1 */ "use strict";
/* 2 */ let a;
/* 3 */ a = 1;
/* 4 */ alert(a);
|type="[]"}
- Line 2
- Line 3
- Line 4
+ None of the above
{Which line(s) lead to an error?
/* 1 */ "use strict";
/* 2 */ let a;
/* 3 */ alert(a);
/* 4 */ a = 1;
/* 5 */ alert(a);
|type="[]"}
- Line 2
- Line 3
|| The alert shows that the variable a does have a value of <code>undefined<code>. This is not considered to be an error.
- Line 4
- Line 5
|| The first alert shows that the variable a does have a value of <code>undefined<code>. This is not considered to be an error.
+ None of the above
|| The first alert shows that the variable a does have a value of <code>undefined<code>. This is not considered to be an error.
{Which line(s) lead to an error?
/* 1 */ "use strict";
/* 2 */ alert(a);
/* 3 */ let a = 1;
/* 4 */ alert(a);
|type="[]"}
+ Line 2
- Line 3
- Line 4
- None of the above
{Which line(s) lead to an error?
/* 1 */ "use strict";
/* 2 */ {
/* 3 */ const a = 1;
/* 4 */ alert(a);
/* 5 */ } //
/* 6 */ alert(a);
|type="[]"}
- Line 2
- Line 3
- Line 4
- Line 5
+ Line 6
- None of the above
Be creative
Explain to your colleague why programs need variables.
Create a code snippet that defines a variable within a block scope. Access this variable within the block scope and outside of the block scope.
Discuss with your colleague the advantages and disadvantages of using the global context.