w3School course

Course

KeywordDescription
varDeclares a variable
letDeclares a block variable
constDeclares a block constant
ifMarks a block of statements to be executed on a condition
switchMarks a block of statements to be executed in different cases
forMarks a block of statements to be executed in a loop
functionDeclares a function
returnExits a function
tryImplements 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ùng var khi code cần support cho các trình duyệt cũ.
  • letconst được thêm vào JS code từ 2015.

Sử dụng const, let, var khi nào?

  1. Always declare variables
  2. Always use const if the value should not be changed
  3. Always use const if the type should not be changed (Arrays and Objects)
  4. Only use let if you can’t use const
  5. Only use var if you MUST support old browsers.

Resources