Local

Local variables are available only within the function. When the function finishes executing, the local variable goes away. When creating variable names inside your function, you don’t have to worry about whether those names already exist outside your function; the scope inside the function takes precedence. However, some global variables you don’t want to overwrite. For instance, the function in Listing 8.20 creates a local variable called console and then tries to use console.log, but it fails because, within the scope of the function, the value of console is the string 'bash', and the string 'bash' has no log method.

Listing 8.20 Choose Your Local Variable Names with Care

function terminal() {  var console ...

Get Learning to Program now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.