DEV Community

Gustavo Henrique Silva Tenório
Gustavo Henrique Silva Tenório

Posted on

3 1

LET and CONST, when should I use?

Hello, DevGuys!

The CONST and LET variables are introduced in ES6 and nowadays it's very common to use in new codes. But, these days I've been studying React JS and a question came to mind, when should I use them?

Well, it's simple... they have your duty in code and help us to understand the codification

CONST means the variable can't be reassigned a new value and declarations are block-scoped. So, you can create to define constants values to be as "ready only". Please don't try to change this value :D.

const UNDERAGE_VALIDATION = 18;
function validateAge(age){
 (age < UNDERAGE_VALIDATION) 
   ? console.log("Underage person")
   : console.log("Older");
}
Enter fullscreen mode Exit fullscreen mode

LET this variable will be reassigned and is block-scoped, that variable can be updated but not re-declared. :).

let counter = 0;
while(counter <= 10){ counter = counter + 1; }
Enter fullscreen mode Exit fullscreen mode

That's simple, keep it's simple and do a clean code.
Bye-bye!

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (1)

Collapse
 
mzaini30 profile image
Zen

const and let are required if you using Svelte

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️