DEV Community

Sedrick Parker
Sedrick Parker

Posted on

Holding values into memory.!

--Memory management
- In my own words, codes are nothing more than values we have stored in memory and constantly perform actions on . we must have a way to attach values into our memory that can be used throughout the short or long lifespan your code will run. To do this, we assign our values to variable either 'var','let' or 'const'. This action will insure that your value is now in the 'memory' of the scope it was created.

//code example: let name = 'Leeroy'

const hasCard = false ;
var ageLimit = 21;

//assigning a variable to the memory of the scope

--the 'let' and 'const' serves a great way of storing values to a particular scope .

  • when using the 'let' keyword your variable will only be available to the scope in which it was created in . As for the 'const' keyword, this is a typically for values not intended to be change . The 'const' keyword cannot be reassigned and neither 'let' or 'const' can be hoisted.

//code example
const teenageClub = (name, age, admissionMessage) =>{
if (age >= 13 && age <= 19){
let valid = 'hey ' + name + ' looks like you can join the teenage club, we will be in touch'
admissionMessage = valid ;
//the valid variable is only available to that scope, so only if the user is a teenager will the valid variable actually exist . if we were to attempt to grab that variable outside of that scope , we will get a 'reference error'

return admissionMessage;
Enter fullscreen mode Exit fullscreen mode

}

return 'you are not accepted'
}

console.log(teenageClub('bill', 17, 'I would like to be a part of the teenage club'))

The 'var' key word is able to be hoisted meaning that its value is in the scope of memory before any code is ran . This is the semi older way to store values but can be any value defined to the console. The tricky thing about the 'var' keyword is they can really cause bugs to your code if you give them the same names as references. (little tip : just don't do that)

//code example

var mypw = '$sjsdskdn';

var mypw = 'idonteenknow';

var mypw = 12;

console.log(mypw);

//this will actually give you a value , the problem with this is how will we know which value we are targeting ? This is where the 'let' and 'const' keyword outshines because you wouldn't be able to do the with those keywords..

  Conclusion 
Enter fullscreen mode Exit fullscreen mode

No matter what you are trying to perform with your app or code, you will be working will values made in a memory . Understanding the different ways it could be used for different scenarios is a great step towards successful development. Thank you.

Top comments (0)