Local and Outer Variables

Part 1: Local Variables

  • Declare a function named showMessage. Declare a local variable message and assign it the value "Hello".

  • Log the value of message.

  • Call the function to display the message.

  • Try to log the message variable outside the function to see if it is accessible.

Part 2: Outer Variables

  • Declare a variable named userName above the showMessage function and assign it the value "John".

  • Append userName to the end of message.

  • Call the function to display the updated message.

Part 3: Local Variable Shadowing

  • Inside the showMessage function, declare a variable named userName above the message variable and assign it the value "Martin".

  • Call the function to display the message and see how the local variable shadows the outer one.

Are you stuck on this problem?

Get detailed, step-by-step solution and improve your JavaScript skills.

  • Detailed Explanation: Each solution comes with a comprehensive explanation, helping you understand the logic and concepts thoroughly.
  • Learn to Write Better Code: Even if you have written the code, discover ways to improve it and adopt best practices.
  • Save Time: Don’t waste hours struggling—get the solution and learn efficiently.
  • Lifetime Access: Get access to lifetime solutions for all problems available
Get the Solution

" I struggled with this problem for hours, but the solution provided here was clear and easy to follow. It helped me understand where I went wrong and improved my coding skills significantly." - Jane D.

If you're not satisfied, I offer a 7-day money-back guarantee.