Doubt in basic of Java Script

Tell us what’s happening:
i am getting answer 5, twice. what’s wrong. even after deleting console.log outside the function.

Your code so far


function myLocalScope() {
 'use strict';

 // Only change code below this line
var myVar=5;
 console.log(myVar);
}
myLocalScope();

// Run and check the console
// myVar is not defined outside of myLocalScope


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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36.

Challenge: Local Scope and Functions

Link to the challenge:

hey koboltz, I could not see your reply.

Hi, the test does pass with two 5s because the console.log is showing the value of myVar inside the function as well as logging the value of myVar when the function is called :smiley:

i have used console.log, only once that to inside the function.

Thank you, for pointing this out. It is very interesting behaviour, and we have been discussing the accuracy of the built-in freeCodeCamp console, over on this GitHub issue.

can you give me solution for my question?

Well, you code passes the tests. So, as far as that goes, you have done exactly as expected.

The reason you get 5 twice is the way the tests are checked, requires the output to be evaluated before and after setting up the console.

you see 5 twice because the function is called once in the editor and then it is also called by the tests

can you explain me in detail? I am a complete beginner.

the function contain the console.log() statement so each time the function is called the console log statement is executed

you have a function call in the editor (localScope())
and then the tests also call the function

1 Like

the function contain the console.log() statement so each time the function is called the console log statement is executed.
(i understand this)

you have a function call in the editor ( localScope() )
and then the tests also call the function. (ie.** i dont understand this**) what is test?

the tests that say if you solved the challenge

to check if your code does the right thing the tests have to execute your code

let me ask you directly , is my code in correct?

i have another question : if i declared without var ie. myvar=5(global scope without var key word); in the same question under function, and if i log console.log(myVar), it should log 5, is it right?

what do the tests say when you run them?

it depends on how strict the environment is, you could get an error that myVar is never declared. Or it could print 5 to the console.

1 Like