DEV Community

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

Posted on

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!

Top comments (1)

Collapse
 
mzaini30 profile image
Zen

const and let are required if you using Svelte