3 Tips To Use Const Variable More Often in JavaScript

Use Const by default, unless it needs to change.

Dev by RayRay
May 6 · 3 min read

We should use the variable more often in JavaScript. I want to share my 3 tips on when you can use instead of using or .

1. Const Variables Usage

A lot of times we found ourselves storing information in a variable, no is not a good fit anymore. (find out why in another post.)

From my experience a lot of times the information doesn’t need to change, so we can use the variable.

Only use if it needs to change, otherwise use the variable by default.

But Ray, what about and they need to be changed a lot of times.

Constants are block-scoped, much like variables defined using the let keyword. The value of a constant can’t be changed through reassignment, and it can’t be redeclared. Source: MDN Web Docs

That’s true! But we can store them in a and still add and remove information because this won’t change the data type. So pick a variable to store the or .

2. For…of and for…in loops: use const variables

We all know these ugly for-loops in JavaScript or any other languages. But since ES2015 there are 2 for-loops which are much easier to write and read.

The for…of and for…in loops are so simple! Days are over that you have to create a variable that has to add a number every iteration.

And the nice thing is, you can use variables in them, which you can’t do with the normal for-loop.

For…of

Image for post
Image for post

The for…of loop is for looping over iterable objects. You can use it with String, Array, array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set, and user-defined iterables.

const arr = ['Angular', 'React', 'VueJS', 'Svelte', '', 'React']for(const item of arr) {
console.log(item)
}

In comparison to the for-loop where the variable is being updated every iteration. The for…of loop the variable is being created with every iteration.

For…in

Image for post
Image for post

The for…in loop is for looping over the properties of an Object.

const object = {a: 1, b: 2, c: 3};for (const property in object) {
console.log(`${property}: ${object[property]}`);
}

Like the for…of loop, the variable is being created every iteration. Don’t use a or here.

3. Store Elements in Const variables

Image for post
Image for post

If you need to access the DOM by using for example, I see a lot of times that people use or .

But if you only want to access the element, it’s better to store it in a variable.

const wrapper = document.querySelector('.wrapper')

Even if you are gonna change the element, is still a good choice. Because the element is an so it can change when needed.

I’ve gathered a couple of aspiring developers around the world on a Discord server, feel free if you like to join in.

Conclusion

I hope that one or more of these tips will help you improve your JavaScript skills. If you have questions or any additions to add here, please let me know in the comments 😉

Undefined Developer

Making an undefined impact on developers 🚀

Thanks to Zack Shapiro

Dev by RayRay

Written by

I write stories about Frontend Dev, JavaScript, Typescript, Angular, NodeJS, Serverless Functions, JAM Stack, FaunaDB, Netlify, Apple, iOS— https://byrayray.dev

Undefined Developer

Making an undefined impact on developers is very important! In the future we need more to build business and services. let’s start with that today!

Dev by RayRay

Written by

I write stories about Frontend Dev, JavaScript, Typescript, Angular, NodeJS, Serverless Functions, JAM Stack, FaunaDB, Netlify, Apple, iOS— https://byrayray.dev

Undefined Developer

Making an undefined impact on developers is very important! In the future we need more to build business and services. let’s start with that today!

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