Variable is the fundamental part in programming. We declare variable to store different data types. To declare a variable we use the keyword _var_, _let_ and _const_. A variable can be declared at different scope. In this section, we will see the scope variables, scope of variables when we use var or let.
Variable is the fundamental part in programming. We declare variable to store different data types. To declare a variable we use the keywords_var_, _let_ and _const_. A variable can be declared at different scope. In this section, we will see the scope of variables. Scope of variables when we use var or let.
Variables scopes can be:
- Global
@ -72,7 +72,7 @@ console.log(a, b) // accessible
### Global scope
A globally declared variable can be accessed everywhere in the same file. But the term global is relative. It can be global to the file or it can be global relative to some block of codes.
A globally declared variable can be accessed everywhere in the same file. But the term global is relative. It can be global to the file or it can be global relative to some block of codes.
```js
//scope.js
@ -131,9 +131,8 @@ Now, you have an understanding of scope. A variable declared with *var* only sco
function letsLearnScope() {
var gravity = 9.81
console.log(gravity)
}
// console.log(gravity), Uncaught ReferenceError: gravity is not defined
console.log(gravity) // Uncaught ReferenceError: gravity is not defined
if (true){
var gravity = 9.81
@ -158,22 +157,22 @@ function letsLearnScope() {
console.log(gravity)
}
// console.log(gravity), Uncaught ReferenceError: gravity is not defined
console.log(gravity) // Uncaught ReferenceError: gravity is not defined
if (true){
const gravity = 9.81
console.log(gravity) // 9.81
}
// console.log(gravity), Uncaught ReferenceError: gravity is not defined
console.log(gravity) // Uncaught ReferenceError: gravity is not defined
for(let i = 0; i <3;i++){
console.log(i) // 0, 1, 2
}
// console.log(i), Uncaught ReferenceError: i is not defined
console.log(i) // Uncaught ReferenceError: i is not defined
```
The scope *let* and *const* are the same. The difference is only reassigning. We can not change or reassign the value of the `const` variable. I would strongly suggest you to use *let* and *const*, by using *let* and *const* you will write clean code and avoid hard to debug mistakes. As a rule of thumb, you can use *let* for any value which change, *const* for any constant value, and for an array, object, arrow function and function expression.
The scope of *let* and *const* are the same. The difference is only reassigning. We can not change or reassign the value of the `const` variable. I would strongly suggest you to use *let* and *const*, by using *let* and *const* you will write clean code and avoid hard to debug mistakes. As a rule of thumb, you can use *let* for any value which change, *const* for any constant value, and for an array, object, arrow function and function expression.
1. Find people who are MERN stack developer from the users object
1. Set your name in the users object without modifying the original users object
1. Get all keys or properties of users object
1. Get all the values of users object
1. Use the countries object to print a country name, capital, populations and languages.
1. Use the [countries object](../08_Day_Objects/08_day_starter/data/countries_data.js) to print a country name, capital, populations and languages.
### Exercises: Level 3
1. Create an object literal called _personAccount_. It has _firstName, lastName, incomes, expenses_ properties and it has _totalIncome, totalExpense, accountInfo,addIncome, addExpense_ and _accountBalance_ methods. Incomes is a set of incomes and its description and expenses is a set of incomes and its description.
2. **** Questions:2, 3 and 4 are based on the following two arrays:users and products ()
```js
** Questions:2, 3 and 4 are based on the following two arrays: `users` and `products`