- Blocks and Scope
- Global Scope
- Block Scope
- Scope Pollution
- Practice Good Scoping
If you are not familier with them please download Learn JavaScript eBook at https://codingwithbasir.com/learn-javascript
- Create a html file with your name like john.html
- Add current code to that file:
<!DOCTYPE html>
<html>
<body>
<script>
// your code here
</script>
</body>
</html>-
Define a global variable named
colorwithconstand set it toblue -
Declare a function named
logColor1without parameter and logcolorvaribale into console. -
Declare a function named
logColor2with a parameter namedcolorand logcolorvaribale into console. -
Call function
logColor2with'red'as argument and check the result. -
Declare a function named
logColor3with a parameter namedcolorand logcolorvaribale into console. -
In this function, add an if statement before
console.logthat checkcoloris green. If it is green useletto define a variable namedcolorand set itlight green, thenconsole.log(color) -
Call function
logColor3with'green'as argument and check the result. -
Analyze and discuss the behaviour of scoping in all steps.
