I Don't Know JS Yet: Declaration and Scope
Tags: javascript, es6, webdev, codenewbie
Published: 2020-11-01 18:05:57

In this first article of the series **['I Don't Know JS Yet'](https://dev.to/thehumzahasan/i-don-t-know-js-yet-1fhc) '**, we will be looking at the various types of variable declaration along with their scope inside a particular program.

###**Declaration and Scope**

Pre ES6 we only had the **'var'** keyword available to us for declaration of variables inside a javascript program, which was said to be **function-scoped**. But after the release of ES6, we got two new keywords **'let'** and **'const'** for variables declaration which is said to be **block-scoped**.

To understand the difference between function scoped and block-scoped, let's look at the below two snippets of code.

![Aye aye cap](https://media1.popsugar-assets.com/files/thumbor/AOEG86oqgN7qR9GUXpVnEoMgQto/fit-in/1024x1024/filters:format_auto-!!-:strip_icc-!!-/2015/04/21/871/n/1922283/4ed5e81f2817033c_cap-leader-1424968038/i/Chris-Evans-Captain-America-GIFs.gif)


####👇*Function scoped behaviour of var*  
```javascript
var fname = "Captain";  //Global Scope
function displayN() {
	var lname = "America";
	console.log(`${fname} ${lname}`);
}

displayN();                                      
console.log(`${fname} ${lname}`);  //ReferenceError: lname is not defined

/*
Output:
Captain America 
*/
```
*Explanation 👉*When the program executes the first variable 'fname' is defined in the global scope (i.e, it is accessible throughout the program) and then the function displayN() is declared and called. Inside this function, the 'lname' variable is declared and accessible only inside the function and any attempt to call it outside the function will result in *'ReferenceError'*.    

#### 🛑Note: Variable defined with 'var' in the global scope as well as inside a function scope can be updated at any point of the program.

***
![Marvel](https://images6.fanpop.com/image/photos/42500000/Captain-Marvel-GIF-marvels-captain-marvel-42588203-540-268.gif)


####👇*Block scoped behaviour of let &const*  
```javascript
let fname = "Captain"; //Global Scope
const color = "Red and Blue";  //Global Scope

function superHero() {
	let lname = "Marvel";
	console.log(`${fname} ${lname}`);
	if(true) {
		let age = 58;
		console.log(`${fname} ${lname} was ${age} years old.`);
		age = 59;  
		console.log(`${fname} ${lname} is ${age} years old.`);
	}
	console.log(`${fname} ${lname} is ${age} years old.`); //ReferenceError: age is not defined
}

superHero();
console.log(`${fname} loves ${color}.`);
color = 'Green';  //TypeError: Assignment to constant variable.
console.log(`${fname} ${lname} `);  //ReferenceError: lname is not defined
/*
Output:
Captain Marvel
Captain Marvel was 58 years old.
Captain Marvel is 58 years old.
Captain loves Red and Blue.
*/
```
*Explanation 👉*When the above snippet runs, the first two variables are declared using 'let' and 'const' respectively. The only difference is that once you’ve assigned a value to a variable using const, you can’t reassign it to a new value. 

Continuing further, we declare a function superHero() and call it, which has the variable 'lname' defined inside it making it block-scoped along with 'age' inside another block. Both these variables are only accessible inside their particular block and any attempt to call them outside the defined block will result in *'ReferenceError'*.*    

#### 🛑Note: Variable defined with 'let' in the global scope or inside a block can be updated at any point of the program whereas 'const' type of variable's value can't be updated as those values are assumed to be constants. 

####**Quick Snapshot**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5nm44mtiknb2djex2tk6.png)


***

Let's Connect on [LinkedIn](https://www.linkedin.com/in/humzahasannit/) || Follow me on [Instagram](https://www.instagram.com/codewithbravopy/)