---
# System prepended metadata

title: Untitled

---


## <a href='#scope' id='scope'>

## Scope </a>

**Scope in JavaScript**

JavaScript has a feature called scope.Understanding scope will make your code stand out, reduce errors and help you make powerful design patterns with it.


**What is Scope?**

Scope is the accessibility of variables, functions, and objects in some particular part of your code during runtime. In other words, scope determines the visibility of variables and other resources in areas of your code.

When you start writing JavaScript in a document, you are already in the Global scope. There is only one Global scope throughout a JavaScript document. A variable that is in the Global scope is defined everywhere in the code.


```javascript
Example (1) :

// the scope is by default global

var name = 'Hammad';
```

- Variables inside the Global scope can be accessed and altered in any other part of the code.

```javascript
Example (2) :

var name = 'Hammad';

console.log(name); // logs 'Hammad'

function logName() {
    console.log(name);  // 'name' is accessible here and in everywhere else
}

logName(); // logs 'Hammad'

```

---

## <a href='#variable' id='variable'> Differences between (Var, Let, Const)

</a>
variable
One of the features that came with ES6 is addition of ``` let ``` and ``` const ``` which can be used for variable declaration. The question now is, what makes them different from ``` var ``` which has been in use ?


**1. VAR**

Before the advent of ES6, var declarations ruled as King. There are issues associated with variables declared with var though. That is why it was necessary for new ways to declare variables to emerge. First though, let us get to understand var more before we discuss one of such issues.

- Scope of var :

   In the next example `var` declarations are globally, that means it can be accessible in every where in the code.

```javascript
// Here, greeter is globally scoped.

 var greeter = "hey hi";

 console.log(greeter)
```

- `Var` variables can be re-declared :
    

```javascript
// Re-declare var

Example (1) :

    var greeter = "hey hi";
    var greeter = "say Hello instead";
    
    console.log(greeter) // result : say Hello instead
```

* `Var` variables can be Updateed : 

```javascript
// Update var

Example (2) :

    var greeter = "hey hi";
    greeter = "say Hello instead";
    
    console.log(greeter) // result : say Hello instead

```

- Hoisting of `var`

  Hoisting is a JavaScript mechanism where variables declarations are moved to the top of their scope before code execution. What this means is that if we do this:

```javascript
 console.log (greeter); // result : undefined
    var greeter = "say hello"
```

It is interpreted as this

```javascript
  var greeter;
    console.log(greeter); // greeter is undefined
    greeter = "say hello"
    
    console.log(greeter); // greeter is say hello
```

So var variables are hoisted to the top of its scope and initialized with a value of undefined.



**2. LET**

Let is preferred for variable declaration now. It's no surprise as it comes as an improvement to the var declarations.

```javascript
Example (1) :

   let greeting = "say Hi";
   let times = 4;

   if (times > 3) {
        let hello = "say Hello instead";
        console.log(hello);//"say Hello instead"
    }
```

- Let can be updated : 

  Just like var, let can be updated. 

```javascript
    let greeting = "say Hi";
    greeting = "say Hello instead";
    console.log(greeting) // result : say Hello instead
```
`Let` declarations are hoisted to the top. Unlike var which is initialized as undefined, the let keyword is not initialized. So if you try to use a let variable before declaration, you'll get a Reference Error.
This will return an error : 

```javascript
console.log(greeting) // Uncaught ReferenceError: Cannot access 'greeting' before initialization.

let greeting = "say Hello instead";
```
- `Let` cann't be redeclared : 


```javascript
// This will return an error : 

let greeting = "say Hi";
let greeting = "say Hello instead";//error: Identifier 'greeting' has already been declared
```

**3. CONST**

Variables declared with the const maintain constant values. const declarations share some similarities with let declarations.

- `Const` cannot be updated or re-declared :

  This means that the value of a variable declared with const remains the same. It cannot be updated or re-declared. So if we declare a variable with const, we can neither do this :

  ```javascript
      const greeting = "say Hi";
      greeting = "say Hello instead";//error : Assignment to constant variable.
  ```

  nor this :

  ```javascript
   const greeting = "say Hi";
   const greeting = "say Hello instead";//error : Identifier 'greeting' has already been declared
  ```

- Hoisting of const : 

Just like let, const declarations are hoisted to the top but are not initialized.

```javascript
console.log(greeting) // Uncaught ReferenceError: Cannot access 'greeting' before initialization.

c greeting = "say Hello instead";
```
