Skip to content

Conversation

@Devonte202
Copy link

No description provided.

Copy link
Contributor

@ROgbonna1 ROgbonna1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really strong submission. Some critical changes to make but they are all pretty much based on one misunderstanding of how var variables are scoped. Make these changes and push them back up this week.

**1. How do variables declared with `let`, `const`, and `var` differ? Be sure to touch on _scope_, _reassignment_, and _hoisting_ for each.**

To begin with, the declaration of all three of these variable types are automatically
hoisted by javascript right before runtime, simply because they are variables.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes!

To begin with, the declaration of all three of these variable types are automatically
hoisted by javascript right before runtime, simply because they are variables.
As for their differences, variables declared with `const`, are designed to
be constant and therefore, JavaScript does not allow it's value to be reassigned.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well said!

On the other hand, using `let` to declare a variable does allow you to reassign
its value later on. `let` and `const` are scoped variables, which means that its
existence can only be detected within its parent scope, whether that be global
or local. As for `var` its properties are the same as `let`, except for the fact
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect. Variables declared with var have a scope as well. Their scopes are defined by functions. We say they are function scoped.

scope as well as in the local scope of the `testScope` function. The value of `a`
that gets logged on lines 24 and 26 is "outer" because that's the onlt value of `a`
that it has access to. Meanwhile "undefined" is logged because the function `testScope`
is referenced, however there is no return value within it, thus "undefined" is logged.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the local variable a defined within the testScope function? Run this in the console and tell me what you get.

console.log(a);
```
This above code will log:
"undefined"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't get undefined because the function hello does not log anything.


sayItLoud();
```
The variable `name` was declared and initialized within the global scope, and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great answer!


let greeting = "Hello"
```
This logs an error, because `let` is a scoped variable, JavaScript cannot access it until
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take it!

console.log(`Status for Problem Set: ${problemSet}`);
```

This code logs "Status for Problem Set: undefined". This is because the variable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Answer is correct but explanation has a critical flaw. problemSet is not initialized in the body of that function. Remember: function parameters become locally scoped variables in the body of the function. Thus, when we assign the parameter a value in the function, it does not impact the binding of the argument passed to that function.


console.log(advice);
```
The variable `advice` is declared locally within the if statement, therefore when
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep

console.log(advice);
```

This code works despite the variable `advice` being declared locally because
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Half correct. Make this change.

Variables declared with var are function scoped, not block scoped. Thus, advice can be accessed outside of the block where it is declared.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants