Understanding debugging for Javascript when using scope, undefined, and reference error

Understanding debugging for Javascript when using scope, undefined, and reference error
0.0 0

#1

Tell us what’s happening:
I don’t understand scope and I need feedback abut why these different examples have local/global scope or if they throw a reference error or undefined. I am trying to understand what scope means so that I can correctly move with the right building blocks to the next tier of understanding, rather than learning sloppy fundamentals.

I have provided the answers but I dont understand what differentiatesd one from the other. This core issue is preventing me from completing the assignments on my own rather than getting spoon-fed answers. I know this is simple, but I think it’s pretty important ( I could be wrong).

I don’t understand what a reference error is or what undefined means and I think this is holding me back in my professional career.

Your code so far

  1. function func() {
    if (true) {
    let a = ‘hello’;
    }
    console.log(a)

  2. The three choices of answers are:
    a.'hello’
    b.undefined
    c.Reference error - this is the answer

  3. function func() {
    if (false) {
    var a = ‘goodbye’;
    }
    console.log(a);
    }

  4. The answer choices are:
    a. undefined - this is the answer
    b. reference error
    c.‘goodbye’

  5. function func() {
    let a = ‘hello’;
    if (true) { let a = ‘goodbye’;}
    console.log(a);
    }

  6. a. undefined
    b. 'goodbye’
    c. ‘hello’ -this is the answer

  7. function func() {
    var a = ‘hello’;
    }
    consle.log(a);

  8. a. 'hello’
    b. reference error - this is the answer
    c. ‘goodbye’ - this is the answer

  9. function func() { let a =‘hello’;
    if (true) {
    let a = ‘goodbye’;
    console.log(a);
    }
    }

  10. a. 'hello’
    b. ‘goodbye’ - this is the answer
    c. undefined

  11. function func(){
    if (true) {
    var a = ‘hello’;
    }
    console.log(a);
    }

  12. a. a
    b. undefined
    c. ‘hello’ - this is the answer

Your browser information:

Your Browser User Agent is: **Chrome/63.0.3239.84**.

Link to the challenge:
https://www.enki.com/ (on the app, the different examples of scope are provided above if you complete the challenges on the app)


#2

Scope is a simple concept, but there are a lot of wrinkles that make it difficult to explain in a forum post. I suggest you read up on it: You Don’t Know JS: Scope & Closures

Basically, scope defines the context in which a value exists. In JavaScript, scope is most often defined by functions - if you define a variable within a function, you can’t access it from outside of the function. Check out question 7

function func() {
    var a = ‘hello’;
}
consle.log(a);  // a has been defined within the function func(). 
//Since it has not been defined outside of the function, 
//you get a ReferenceError when calling console.log()

This is true so long as you use the var keyword. The new ES6 keywords, let and const, are block scoped. This means they are only defined within the closest curly braces. Often, this is still a function, but it could also be an if clause, a for loop, or just some arbitrarily placed curly braces.

function func() {
    let a = ‘hello’;
    if (true) { let a = ‘goodbye’;} // this definition only exists within the curly braces.
    console.log(a); // 'hello'
}

All of these questions are variations on the same theme. Where is the variable defined?