Coding task · JavaScript and TypeScript course · Module 2: Functions, Arrays and Objects
Exercise: Variable Scope
A global variable is visible everywhere, a variable declared in a function only in that function, and let and const declared in a { } block only in that block.
Task
Declare a global constant parkName = 'Jurassic Park'. Write a function checkEnclosure() that creates a local constant enclosureName = 'Zone A' and, inside an if (true) block, creates a variable alarmLevel = 5 with let and displays parkName, enclosureName and alarmLevel in the console. The function returns enclosureName. Call checkEnclosure(). Outside the function, enclosureName and alarmLevel must not exist.