[TypeScript] Let vs Const vs Var

When to use each of these?

FAM
FAM
Sep 30, 2020 · 2 min read

All have started with var…

Plain Javascript has only a keyword that allows you to define a variable.

Here is an example of how we can use it:

Will the last line of code passe or fail?

Well, there is no problem with the code since the var keyword is not block-scoped. This means there is no scope for variables that are declared with var, whether they are declared inside or outside of a loop or a condition.

To resolve this problem, you need to declare you variable with let or const.

‘Let’ comes to solve the issue

The statement declares a block-scoped local variable, optionally initializing it to a value.

This time, the last line will fail since variable is declared with let inside if condition block, thus block scoped.

But, sometimes we need not only block-scoped variables but also immutable ones. Here comes the limitation of declaration.

When we need an immutable variable …

is the only way to declare a block-scoped immutable variable. This is generally a best practice when we are going to declare a variable that we won’t reset afterward. Immutable is generally a good approach that leads to fewer bugs and maintainable code.

Here is an example of usage:

In this example, I show two cases. The first variable can’t be reset to null since it’s declared with the keyword.

The second case is declaring a object. this time only the object reference is immutable that’s why we can’t set but we can set .

💼Takeouts

  • The keyword is not block scoped
  • A local variable is not possible with keyword
  • The keyword should not be used while building a new typescript application.
  • The keyword is block scoped
  • is for block-scoped variables and immutable variables
  • For objects, only its reference is immutable.

Dear readers and friends, thank you for your support and your precious time. I hope this was useful and helpful for you.

Follow me on Medium and Linkedin for more articles.

See you soon (ノ◕ヮ◕)ノ*:・゚✧

FAM

The Startup

Get smarter at building your thing. Join The Startup’s +725K followers.

FAM

Written by

FAM

I’m a passionate software engineer and woman in the tech world. Find more about me here: 🔗 www.fam-front.com. I love to help people and share what I know!

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +725K followers.

FAM

Written by

FAM

I’m a passionate software engineer and woman in the tech world. Find more about me here: 🔗 www.fam-front.com. I love to help people and share what I know!

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +725K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store