In javascript, the variable scope and its hoisting are quite different.Let’s take an example – var car = “Mercedes” // variable is defined in the global scope
function car_name () { // function
car = “BMW”; // this is a local scope but the variable car is global variable
var car = “Benteley” // this is a variable that is defined in local scope and is local variable
} First, let’s talk about what global and local scope is – Global Scope (Global Variables)All the variables declared outside the function are in the global scope and are available in the entire HTML document.
We can also access the global variable with the help of the window object as their global context is window object.