w3School course
Course
Keyword | Description |
---|---|
var | Declares a variable |
let | Declares a block variable |
const | Declares a block constant |
if | Marks a block of statements to be executed on a condition |
switch | Marks a block of statements to be executed in different cases |
for | Marks a block of statements to be executed in a loop |
function | Declares a function |
return | Exits a function |
try | Implements error handling to a block of statements |
var
keywords được sử dụng trong JS code từ 1995 - 2015. Hiện nay, chỉ nên dùngvar
khi code cần support cho các trình duyệt cũ.let
vàconst
được thêm vào JS code từ 2015.
Sử dụng const
, let
, var
khi nào?
- Always declare variables
- Always use
const
if the value should not be changed - Always use
const
if the type should not be changed (Arrays and Objects) - Only use
let
if you can’t useconst
- Only use
var
if you MUST support old browsers.