freeCodeCamp Challenge Guide: Local Scope and Functions

Local Scope and Functions


Problem Explanation

Local scope means that the variable is available within a certain area. In the case of this exercise, myVar is only available within the function, and not anywhere outside.


Solutions

Solution 1 (Click to Show/Hide)
function myLocalScope() {

  // Only change code below this line
  var myVar;
  console.log('inside myLocalScope', myVar);
}
myLocalScope();

// Run and check the console
// myVar is not defined outside of myLocalScope
console.log('outside myLocalScope', myVar);

Code Explanation

  • The variable only exists in the function. Outside the function, it is non-existent.
25 Likes

Answer is very simple -

function myLocalScope() {
  var myVar = "foo";
  
  console.log(myVar);
}
myLocalScope();
9 Likes

// Declare my global variable here
var myVar = 5;
// or myVar = 5;

function myLocalScope() {
var myVar = ā€œJoā€;

console.log(myVar);
}

myLocalScope(myVar);
myLocalScope();
console.log(myVar);

//how pass the test?

4 Likes

function myLocalScope() {
var myVar = ā€˜use strictā€™;
console.log(myVar);
}
myLocalScope();

5 Likes

Iā€™ve been stucked on this challenge for a week and my hardwork paid off and of course with the help of FCC/Help Gitter community. Thanks to @thekholm80

Lessons learned to pass this challenge:

  1. Do not delete/modify anything inside the myLocalScope() function - you will see that there is a 'use strict' line. You need to ignore that. Before I thought that is the string value that we need to assign in the myVar variable, but itā€™s not.

  2. Assign value to myVar inside the myLocalScope function. You can assign either string or numeric values on it.

  3. Lastly, read the commented out instructions on the console like / / Run and check the console AND // myVar is not defined outside of myLocalScope

The most important of all is // Now remove the console log line to pass the test Ignoring this instruction will not make you pass the challenge.

Hope this hints made you pass the challenge. Remember always Read-Search-Ask

18 Likes

Well, itā€™s really easy if you follow all the Instructions and Hint
Refreshing the page may help if you get stuck.
Try!
Here is my spoiler (click on the blur :point_down: )

[spoiler]function myLocalScope() {
ā€˜use strictā€™;

var myVar = 100;
console.log(myVar);
}
myLocalScope();

// Run and check the console
// myVar is not defined outside of myLocalScope
//console.log(myVar);

// Now remove the console log line to pass the test

[/spoiler]

14 Likes

\This was the most annoying challenge

var myVar = 5;
function myLocalScope() {
ā€˜use strictā€™;
var myVar = 5;

console.log(myVar);
}
myVar = myLocalScope(myVar);

// Run and check the console
// myVar is not defined outside of myLocalScope
console.log(myVar);

// Now remove the console log line to pass the test

12 Likes

im just commented this string outside function //console.log(myVar);
because it not correct.

1 Like

I had to check this because i couldnā€™t understand the problem goal

4 Likes

Iā€™m trying to use console.clear() but donā€™t work in firefox. maybe could be that?

1 Like

Iā€™m not sure about that but these code should run anyway in FireFox. Can you please refresh your browser?

2 Likes

peep this code right hurrr esp. the part where daniel created the code var myVar and myVar = (making it global)

1 Like