Today is the Chocolate Day and today I will spoke about my lover Js ,how she helped and solved the conflict among my mother and father over their glasses. In my family, my mother and father both have near sightedness and far sightedness , so both needs to wear glasses and since both doesn't like to wear a single glass solving both problem(near and far sightedness), they made 2 glasses ,one for near and other for far sightedness.
So, my father sometimes got confused with the boxes which one has near sightedness glass or which one has far-sightedness? So, I asked my lover js ,how we can solve my father problem. As she being too intelligent, she introduced me to the concept of variables.She told me to stick a named paper to each box- one named as "nearSightednessBox" and other named as "farSightednessBox" and kept the respective glass at respective named box and in this way dad can easily identify the glass.
In her language,I know about "var" only ,So i declared two variables and assigned them required value.
var nearSightednessBox = "dad-glass1";
var farSightednessBox = "dad-glass2";
console.log(nearSightednessBox);
console.log(farSightednessBox);
dad-glass1 dad-glass2
This solves my dad problems,but after 2 days another problem came to picture. As my mother also have 2 glasses , so she also demanded to make the same boxes labeling for her glasses too.But how can I give same box name to my mother glasses, since it will conflict with the dad boxes. So I tried to use my mind this time ,without taking help of my lover js , I brainstormed and got an idea that why not I place my dad's glass boxes at one room of the house and my mother glasses to anywhere in the house, so no conflict will be there. So I kept it like this.
//indicating one room in a house since using {} curly braces
//{} creating a block here
{
var nearSightednessBox = "dad-glass1";
var farSightednessBox = "dad-glass2";
}
//my mother glasses anywhere in the house
var nearSightednessBox = "mom-glass1";
var farSightednessBox = "mom-glass2";
console.log(nearSightednessBox);
console.log(farSightednessBox);
mom-glass1 mom-glass2
And now as I was assuming that problem got solved. It becomes huge.As whenever dad or mom, are calling nearSightednessBox or farSightednessBox they are getting mom-glasses only. So both of the got confused. I will not lie, I also got confused along with them. So here comes the wonder JS-Javascript came to rescue and solve the problem.
She told us that she have a better solution but for that mom and dad have to go to specific room only and from there they can access there glasses easily and then no conflict over the glasses will be there and also even if they try to access the glasses from outside the room, they wouldn't be able to.Both agreed to this and then JS brought another sword form her list of swords and that was "let" and "const" variables.
//indicating one room in a house ,
//containing my dad's glasses since using {} curly braces ,
//{} creating a block here
{
let nearSightednessBox = "dad-glass1";
let farSightednessBox = "dad-glass2";
console.log(nearSightednessBox);
console.log(farSightednessBox);
}
//indicating another room in a house ,
//containing my mom's glasses since using {} curly braces ,
//{} creating a block here
{
let nearSightednessBox = "mom-glass1";
let farSightednessBox = "mom-glass2";
console.log(nearSightednessBox);
console.log(farSightednessBox);
}
dad-glass1 dad-glass2 mom-glass1 mom-glass2
And this solved the problem but even after solving the problem, she smiled at me and say You know what there is some problem in the code I have written , though its working fine now but can create a problem in future.Can you identify? After listening to this, I got numbed as I don't have any idea. So she told us , right now nearSightednessBox and farSightednessBox variables we declared using "let" keyword and place them at each room/block,"since let has block scope" so a variable declared as let can't be accessible outside the room/block scope and that's perfectly fine.But what if dad kept tomorrow kept a pen in the glassboxed or what if mom kept her makeup kit ,then the whole problem will remain the same.And after listening this,we think yes the problem will be remain the same.So, she gave counter solution for that and that was declare them as "const" instead of "let" and since both let and const have block scope, so mom and dad have to go to specific room/block to access them and with the const variable , they can't reassign the value.Even if they try, it will give an "TypeError: Assignment to constant variable". just like the below code will give error.
{
const nearSightednessBox = "dad-glass1";
const farSightednessBox = "dad-glass2";
nearSightednessBox = "pen";
}
nearSightednessBox = "pen"; TypeError: Assignment to constant variable.
Scope essentially means where these variables are available for use. So, finally Js teaches us that there are 3 ways to declare a variable - var,let and const. var has function scope when declared inside a function(so, a variable declared using "var" keyword inside a function can be accessed inside the function.) Every variable declared outside of the functions have a global scope, it indicates that it can be accessed, from any point in the program and hence variable declared using "var" keyword in this case has global scope. So, in general var has function or global scope.
let and const have block scope only.(ie. variables can be accessed in block only ). Reassignment allowed in the variables which declared as let but not in the const.
Redeclaration(means that we can declare the variable again) is allowed in variable declared using "var" keyword , but not allowed in "let" and "const" variables.
So coming back to today ,as today is the Chocolcate day, so my lover js being very clever, demanded 5 different chocolate boxes. So I thought why not I bought a single empty box and named it as "Chocolate Box" and bought 5 Chocolate as open and I will use for loop and in each iteration, i will keep the chocolate inside the box and present to her as this will save my lot of money.Yeah!Sometime I act frugal.
let chocolateBox;
let count =0;
do{
//i kept the chocolate inside the chocolateBox
//so keep on reassigning the chocolateBox variable
chocolateBox = `Chocolate ${count+1}`;
console.log(chocolateBox);
count++;
}while(count<5);
Chocolate 1 Chocolate 2 Chocolate 3 Chocolate 4 Chocolate 5
But as you already know from the above stories and last 2 days stories that my lover js is too smart.Oh you haven't read my Rose day and propose day story , here are the links for you to read them and know my lover Js smartness.
Rose Day-Feb 7th- Conditionals with my Lover JS (JavaScript) devkeycode.hashnode.dev/rose-day-feb-7th-co..
Propose Day-Feb 8th- Loops with my Lover JS (JavaScript) devkeycode.hashnode.dev/propose-day-feb-8th..
Since, Js acts too smart, she put a restriction that I can't change the content of the chocolateBox and she need 5 chocolates and this made me to leave my frugal nature and bought 5 different chocolate boxes for her. You know what I have used "const" instead of "let" here , so that she can't put other content in it.
const chocolateBox1 = "Ferrero Rocher Chocolate";
const chocolateBox2 = "Ferrero Rocher Chocolate";
const chocolateBox3 = "Ferrero Rocher Chocolate";
const chocolateBox4 = "Ferrero Rocher Chocolate";
const chocolateBox5 = "Ferrero Rocher Chocolate";
console.log(chocolateBox1);
console.log(chocolateBox2);
console.log(chocolateBox3);
console.log(chocolateBox4);
console.log(chocolateBox5);
Ferrero Rocher Chocolate Ferrero Rocher Chocolate Ferrero Rocher Chocolate Ferrero Rocher Chocolate Ferrero Rocher Chocolate
But this foolishness I can only do, as this prevents to change the content of the box in the future and I will have to buy another 5 different boxes to place the items.You don't make this mistake,Ok. Be clever while using let and const.