Chuyển đến nội dung
Skillpedia
Website và giao diện (frontend)75k · #2781

const-let

Skill này làm gì

Skill này kiểm tra JavaScript để tìm khai báo var và gợi ý dùng const hoặc let thay thế. Skill dành cho lập trình viên muốn code an toàn hơn và dễ đọc hơn.

Khi nào nên dùng

  • ✓Khi bạn thấy var trong một tệp JavaScript cũ.
  • ✓Khi một biến có vẻ truy cập được ở nơi không nên.
  • ✓Khi bạn rà soát code để làm rõ trước khi phát hành.

Liên kết skill

https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/const-let/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/const-let/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Hãy đọc qua script này và thay mọi var bằng const hoặc let.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Agent liệt kê mọi khai báo var kèm số dòng, rồi dùng const cho giá trị không bao giờ đổi và let cho giá trị được gán lại.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

Prefer const and let over var

var's function scope and hoisting behavior regularly causes bugs where variables are accessible outside the block they were declared in. const and let enforce block scope and make code intent clearer — const signals that a binding should not be reassigned, which helps readers understand data flow.

Quick Reference

  • Use const for values that are never reassigned
  • Use let when reassignment is needed
  • Never use var — it has function scope and is hoisted, causing subtle bugs
  • const does not mean immutable — objects and arrays declared with const can still be mutated

Check

Scan this JavaScript file for any use of var and flag every occurrence with line numbers.

Fix

Replace all var declarations with const or let as appropriate. Use const when the variable is never reassigned, let otherwise.

Explain

Explain why const and let are preferred over var, covering scope, hoisting, and temporal dead zone.

Code Review

Review scripts, client components, and browser execution paths related to Prefer const and let over var. Flag exact imports, event handlers, runtime side effects, or blocking operations that violate the rule, and state how the change should be verified in the browser.


For full implementation details, code examples, and framework-specific guidance, see references/rule.md.

Rule page: https://frontendchecklist.io/rules/javascript/const-let

Câu hỏi thường gặp

Vì sao var có rủi ro?

Skill nói var có phạm vi hàm và hoisting, có thể gây ra lỗi khó nhận ra.

Khi nào nên dùng const?

Khi biến không bao giờ được gán lại.

const có làm đối tượng không thể thay đổi không?

Không. Skill lưu ý các đối tượng và mảng khai báo bằng const vẫn có thể bị thay đổi.

Chi tiết

Mô tả gốc của tác giả

Use when reviewing scripts, client components, bundles, or runtime behavior related to Prefer const and let over var. Inspect both source code and the browser execution path so fixes target the real bottleneck or bug.

repo: thedaviddias/Front-End-Checklist

Độ phổ biến

Sao GitHub
75k
Vị trí xếp hạng
#2781
Giấy phép
MIT
Cập nhật
31/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự