shou2017.com
JP

Understanding JavaScript's const and let

Mon Dec 31, 2018
Sat Aug 10, 2024

Quick Overview of const and let

const

In a nutshell, use const for values that aren’t meant to change. In other words, constants.

let

Use let for values that might change.

With ES6, we’ll rarely use var anymore.

Examples

Using the old var approach:

var name = 'Taro';
var title = 'Salesman';
var hourlySalary = 5000;

With ES6:

const name = 'Taro';       unlikely to change
let title = 'Salesman';     might change
let hourlySalary = 5000;   might change

Using const and let makes your code more readable, especially for others.

A common practice is to initially write everything with const and then switch to let if you find you need to change the value later.

改訂新版JavaScript本格入門 ~モダンスタイルによる基礎から現場での応用まで

See Also