# "VAR" in JavaScript

In day-to-day life, we code a lot. 🤝 It is nice to meet you if you are a person who spends some time choosing a good name for the variable, just like me. We stay safe by alternating between **"let"** and **"const"** but **"var"** is helpful in handling certain use cases, and knowing it in depth will help us be more clear

## "Var" and it's RULES

* The default value of **VAR** is **"undefined"**
    
* **Var** can be used as a **global scope** and **function scope**
    
    **NOTE** : (**global scope** can be called anywhere. If a variable is declared outside a function then it can be called inside a function and used)
    
    (**function scope** -&gt; only with-in function. If a **var** is declared inside a function then it cannot be used outside it)
    
* It can be **re-assigned** and **re-declared**
    
* **Var** is **Hoisted** (**i.e.,** <mark>Hoisting is a mechanism by which the variables(with VAR) and (function declaration) will move on top of the scope before the code executes</mark>)
    
    **NOTE:** <mark>Only the </mark> **<mark>function declaration</mark>** <mark>is </mark> **<mark>hoisted</mark>** and ***<mark>not the function expression</mark>***
    
* **Duplication of variables** will **not trigger an error** even in **"strict mode"**
    
* Variables will not lose their value unless we initialize a new value (**i.e.,** var a=3; a=4; =&gt; now "a" is 4)
    

## Creating the variable"var"

Variables can be created and used in 2 ways

1. **Traditional way**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1676695806682/8d9625e9-919e-4e63-89d8-8f27fcc11ded.png align="center")
    
2. **Destructured way** to declare a variable **(99.9% we don't use such type of var declaration)**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1676696044617/f22d0a3d-8b9e-45fb-8b79-f53111192907.png align="center")
    

## Variable declaration and initialization

* **<mark>variable declaration</mark>** is defining a **variable without any value**
    
    **Eg :** var **x** ;
    
* **<mark>variable initialization</mark>** is **assigning a value** for the variable that we declared
    
    **Eg :** var **x** = 5 ;
    

## Visibility of variable

Variables declared with **"var"** and **"unqualified variables"** **alone will get added** as a **property of the global object** (**i.e.,** the **"window"** **object**). <mark>LET and CONST will not get added</mark>

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1676697977198/f5c24060-9819-480b-94ff-4ab3a26e2733.png align="center")

## Undefined vs Not-defined (Reference Error)

**<mark>Undefined</mark>**: Calling a variable before it is initialized with a value

**This happens in two scenarios:**

1. If we just declared the variable and called it
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1676697088156/fd922ddc-695d-4ee8-8cba-234f62b3fd6e.png align="center")
    
2. If we call a variable before it is declared and initialized
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1676696862534/0c2d3bd6-d534-46fd-8d76-aadcd100e447.png align="center")
    
    **<mark>Not-defined</mark>**: if we try calling a variable that does not exist at all
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1676696950409/f958ca0c-0cae-41a7-ab95-b820d815e97b.png align="center")
    

## Variables in action (Eg code)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1676697259195/52d92f7b-dc23-4354-b7f6-e1a4e017d6a9.png align="center")

## How do I find if a variable is globally scoped?

1. type **"window"** in the **Console**
    
2. check by using ==&gt; **this.VARIABLE\_NAME**; (**i.e.,** var x = 3 ; this.x)
    
3. **Object.hasOwn(this, "VARIABLE\_NAME")** ==&gt; this will result in **TRUE** **if global** (***or***) **FALSE** if it is not a global variable
    

## SECRET of "var" over GLOBAL CONTEXT

If you are using **"VAR" globally**, it will be **added** as a **non-configurable property of the global object**. As a result, it **cannot be deleted** with the **"delete"** keyword

The **"delete"** operator **can only delete properties** in an **object** **if they are "qualified" as "configurable"** - which means **configurable** should be **"true"**

1. **Example of "VAR"**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1676697637819/33236ae1-da0f-45c9-812c-b3a2f9fa12e7.png align="center")
    
2. **Example of "LET"**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1676697659742/8d7e10d0-dc11-4a89-9acf-654bff33a4dc.png align="center")
    

## Initializing many variables

<mark>Remember, even if we define many variables, their values will get assigned only after initialization</mark>

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1676698329831/c62ad69b-e710-40d4-ac6d-e3c2a47b9b0f.png align="center")

Thank you very much if you came this far. I assume this blog has provided in-depth knowledge of the **"var" keyword**. Please share if you find this interesting.

Please feel free to reach me on [**Twitter**](https://twitter.com/socalled_dev), [**LinkedIn**](https://www.linkedin.com/in/ranjan-r-bb343b130/)
