Wrapping Up

const and let are far better replacements for the old var to define variables. Moving forward, we should quit using var and use const where possible and choose let otherwise. These two have block scope, prevent accidental redefinition of variables, and offer much better programming safety. You also learned how to honor immutability when programming. Next you will learn about improvements in passing arguments to functions.

Exercises

The following exercise problems will let you hone your skills about using use strict, avoiding var, and when to use let rather than const. Give these a try before heading over to the next chapter. You can find answers to these exerciseshere.

Exercise 1

What’s the output of this code?

 function first() {
  for(i = 0; i < 5; i++) {
  second();
  }
 }
 
 function second() {
  for(i = 0; i < 3; i++) {
  console.log(i);
  }
 }
 
 first();

Exercise 2

First modify the code in the previous exercise so it gives a runtime error due to the major issue. Then modify the code to produce a reasonably desired result.

Exercise 3

What are the benefits of using ’use strict’;?

Exercise 4

Will this code result in an error?

 const fourth = '4th';
 
 fourth = fourth;
 
 console.log(fourth);

Exercise 5

Will this code result in an error?

 'use strict';
 
 const person = Object.freeze(
  {name: 'John Doe', address: { street: '101 Main St.', City: 'New York' }});
 
 person.address.street = '102 Main St.';
 
 console.log(person);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset