If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In JavaScript, functions have their own scope, which means that any variables declared inside the function are only accessible within that function's scope. This is known as function scope.
For example, consider the following code:
scssCopy code
function myFunction() { var x = 10; console.log(x); } myFunction(); console.log(x);
In this code, we declare a function called myFunction
that sets the value of a variable x
to 10 and then logs it to the console. We then call the function and also try to log the value of x
outside the function.
When we run this code, we will get an error when trying to log the value of x
outside the function because x
only exists within the scope of the myFunction
function.
It's important to note that variables declared outside of a function (i.e. global variables) are accessible from within any function in the same JavaScript file, unless they are shadowed by a variable declared within the function.
For example:
javascriptCopy code
var y = 5; function myFunction() { var y = 10; console.log(y); } myFunction(); console.log(y);
In this example, we declare a global variable y
and then declare a local variable y
inside the myFunction
function. When we call the function, it logs the value of the local y
variable (which is 10), and when we try to log the value of y
outside the function, it logs the value of the global y
variable (which is 5).
It's generally considered good practice to limit the use of global variables and to keep variables as local as possible to the functions where they are needed, in order to avoid naming collisions and other potential issues.
Comments: 2
I am not able to access videos from second class and further. I have already completed first class
When will I get my course?
Now, Your query was resolved.