Chuyển đến nội dung
Skillpedia
Lập trình chung75k · #3066

type-coercion

Skill này làm gì

Skill này rà soát mã JavaScript tìm các phép so sánh và chuyển đổi có thể cho kết quả bất ngờ. Skill dành cho lập trình viên muốn ít lỗi ẩn hơn.

Khi nào nên dùng

  • ✓Khi mã JavaScript dùng == hoặc != thay vì so sánh nghiêm ngặt.
  • ✓Khi các giá trị được cộng lại theo cách trộn lẫn chữ và số.
  • ✓Khi một đợt rà soát mã yêu cầu xử lý kiểu dữ liệu rõ ràng hơn trong một script.

Liên kết skill

https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/type-coercion/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/type-coercion/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

“JavaScript của tôi đôi khi so sánh giá trị sai. Bạn có thể tìm các phép so sánh lỏng và đổi chúng thành nghiêm ngặt không?”

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

Cách hoạt động

Agent tìm các phép kiểm tra bằng lỏng và các thao tác trộn kiểu rủi ro. Agent thay chúng bằng so sánh nghiêm ngặt và chuyển đổi rõ ràng.

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.

Avoid implicit type coercion

JavaScript's implicit type coercion produces results that look like bugs to anyone who doesn't know the exact coercion rules. == comparisons with mixed types silently convert values in ways that fail to catch genuine errors. Explicit conversions make code intent clear and catch real type mismatches at the point where they occur.

Quick Reference

  • Always use === and !== instead of == and !=
  • Convert types explicitly with Number(), String(), Boolean(), parseInt()
  • Be careful with + operator — it concatenates strings but adds numbers
  • Use Array.isArray() instead of typeof for array checks

Check

Find all uses of == and != (non-strict equality) in this file. Also look for implicit type conversions from + operator and other coercion traps.

Fix

Replace all == with === and != with !==. Add explicit type conversions where mixed types are intentionally compared.

Explain

Explain JavaScript's implicit type coercion rules, why == is problematic, and how to write safe type comparisons.

Code Review

Review scripts, client components, and browser execution paths related to Avoid implicit type coercion. 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/type-coercion

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

Vì sao == là vấn đề?

Nó tự động chuyển đổi giá trị trước khi so sánh, điều này có thể che giấu lỗi thật và khiến kết quả khó đoán.

Cách an toàn hơn để kiểm tra mảng là gì?

Skill khuyên dùng Array.isArray() thay vì typeof để kiểm tra mảng.

Nên chuyển đổi kiểu như thế nào?

Dùng các hàm rõ ràng như Number(), String(), Boolean() hoặc parseInt().

Chi tiết

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

Use when reviewing scripts, client components, bundles, or runtime behavior related to Avoid implicit type coercion. 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
#3066
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ự