Scopes in javaScript

Hi everyone :raised_hand:
for the purpose of learning and practicing the scope in javascript is it possible to create a global variable inside a function when it is called?,
I’ve tried this in my code editor and it gives me an error

function setBestCustomer() {
   return  var bestCustomer = "not bob"

unreachable  code 

Why not create the global variable outside the function and then assign it a value within the function? Yes, you can create a global variable inside a function, but in general it is considered best not to do that. Instead, always explicitly declare variables that you need for your application. In fact, it would be best to create as few global variables as possible unless they are constants that will not change throughout the application.

1 Like

thank you @camperextraordinaire I was curious about it but your answer made it clear now.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.