- 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
color
withconst
and set it toblue
-
Declare a function named
logColor1
without parameter and logcolor
varibale into console. -
Declare a function named
logColor2
with a parameter namedcolor
and logcolor
varibale into console. -
Call function
logColor2
with'red'
as argument and check the result. -
Declare a function named
logColor3
with a parameter namedcolor
and logcolor
varibale into console. -
In this function, add an if statement before
console.log
that checkcolor
is green. If it is green uselet
to define a variable namedcolor
and set itlight green
, thenconsole.log(color)
-
Call function
logColor3
with'green'
as argument and check the result. -
Analyze and discuss the behaviour of scoping in all steps.