JavaScript/Variables/Exercises

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Topic: Declaration and Initialization

1 Which line(s) lead to an error?

/* 1 */ "use strict";
/* 2 */ let a = 0;
/* 3 */ a = 1;
/* 4 */ const a = 10;
/* 5 */ alert(a);

Line 2
Line 3
Line 4
Line 5
None of the above

2 Which line(s) lead to an error?

/* 1 */ "use strict";
/* 2 */ let a = 0;
/* 3 */ a = 1;
/* 4 */ const b = 10;
/* 5 */ b = 100;

Line 2
Line 3
Line 4
Line 5
None of the above

3 Which line(s) lead to an error?

/* 1 */ "use strict";
/* 2 */ let a;
/* 3 */ a = 1;
/* 4 */ alert(a);

Line 2
Line 3
Line 4
None of the above

4 Which line(s) lead to an error?

/* 1 */ "use strict";
/* 2 */ let a;
/* 3 */ alert(a);
/* 4 */ a = 1;
/* 5 */ alert(a);

Line 2
Line 3
Line 4
Line 5
None of the above

5 Which line(s) lead to an error?

/* 1 */ "use strict";
/* 2 */ alert(a);
/* 3 */ let a = 1;
/* 4 */ alert(a);

Line 2
Line 3
Line 4
None of the above

6 Which line(s) lead to an error?

/* 1 */ "use strict";
/* 2 */ {
/* 3 */   const a = 1;
/* 4 */   alert(a);
/* 5 */ } //
/* 6 */ alert(a);

Line 2
Line 3
Line 4
Line 5
Line 6
None of the above


Be creative
  1. Explain to your colleague why programs need variables.
  2. 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.
  3. Discuss with your colleague the advantages and disadvantages of using the global context.