Skip to main content

The ever so common var, let and const

 So in JavaScript world, in whichever way you go, you finally end up with some very basic concepts.

 

I skipped certain basics while learning JavaScript. Later in life, I regretted it, but then, of course, you don't repeat the same mistake twice.


So the very common and pretty basic thing you could see in JavaScript is the concept of var, let and const.

 

What are they? How do they differ from each other?

 

Simply put var, let and const are keywords within JavaScript.


All three of them are used to declare a variable. So why do we need three of them?


JavaScript is weird. You can even use a variable without initialization in JavaScript. So why take the time to write a keyword.


Now for JavaScript, there is a concept called global scope. In a simple browser where you could see JavaScript quite common, the global scope refers to the window object. And in the NodeJs module, this will be the global scope of the module.

So if you simply don't initialize a variable JavaScript looks at the global scope, even though you never even thought about scoping your variable globally.


So these keywords do matter then. 

But which one to use.


var

var is function scoped, and even if you tried to call it after the initialization you get the value as undefined.

 

console.log(hoisted)

var hoisted


The above code will be interpreted by the engine as below:

 

var hoisted 

console.log(hoisted) 

 

So the output that you are going to get is simply undefined. 


let

in case of let, it's all about block scoped. So everything inside a block stays inside.

Also in case of the above code, we run for var will be converted reversely, but unlike var giving you an undefined, now you'll get a reference error

 

const

In the case of const, it's pretty simple. you cannot re-assign values to a variable that you initialized using const. And also they are block-scoped.



So which one to use.


There are many theories and many styles. But I use let and const. 

Some people do initialize with const first then upon situation were they need to re-initialize the variables, they change the code to make it initialized using let.

 

So which one are you going to use? I know this is pretty simple, and not much explained, but basically, this will help you get started with the weird way of JavaScript.


Thanks

Comments

Popular posts from this blog

A smoooooth operation....

 Backward compatibility...   A word that I used to hear when I started my career. You design your APIs with backward compatibility in mind, don't break anything when you are upgrading, think about this, think about that etc. Well, those teachings from my previous mentors didn't go in vain, as I made a fundamental change in how we report problems @  Gelato .    You see recently @  Gelato , the CS (Customer Support) team moved from A ticketing management system to B ticketing management system, which is a monumental task for all the people involved in the CS team. Even though the fundamental concept remains the same the places, the attributes the concepts, and the naming of different attributes all change if you have this transition. And thus it was a big change for the whole company.    After the decision was taken, the first step was to create a well-written transition document, which the good folks at the CS team tackled. Special thanks to  ...

My experience with the Golden signals

In June 2022, I embarked on a quest for a new job opportunity. Fortunately, this endeavor began just before the global job market experienced a significant downturn. I must admit, I faced my fair share of rejections during this period, but I also had an epiphany. It became evident that there was so much more to learn and understand in the world of technology. Coming from a small service-based company, I had encountered limitations in terms of how much I could learn on the job. However, during interviews and conversations with senior developers, I gained valuable insights into the architectural and technical decisions made by teams in various companies. One such company that left a lasting impression on me was Delivery Hero. Their technical blog posts provided a wealth of information, especially for someone like me, transitioning from a smaller company where projects had minimal daily active users compared to the scale of Delivery Hero. One particular blog post that caught my attention ...

A bit laziness can make you productive

 So recently there was this requirement from the operations team at my current company to keep up an excel sheet for the tasks that we do every day. Now my company's work culture is pretty good actually. They never sneak up on you to check whether you are doing your tasks or not. They are cool and most importantly they trust you with your tasks.  This was more like a record for safekeeping, for both parties. All was good except we should maintain this at least weekly. So at the end of the week if the operations team need to cross-check your task time with your leaves all the sheet should be updated.  Now see there are many tools out there that can do this pretty easy for you but trust me you don't want to spend a monthly/yearly fee for such a tool that does so little as keeping up the tasks hour and the project associated with that task. So many times a problem arises for me since I forget things pretty easily. At the end of the week, I need to fill up the entire task rec...