🌐 Frontend & Web

JavaScriptのsubstring():クセ強くて便利、超紛らわしい

JavaScriptのsubstring()、無害に見える。引数入れ替えとNaN許容でベテランすら引っかかる。

JavaScriptの文字列インデックスとsubstring抽出範囲を示す図

⚡ Key Takeaways

  • substring()は引数自動交換、NaN/負許容——便利だがバグ隠す。
  • 負や予測性ならslice()優先、レガシー許容ならsubstring。
  • インデックス常に検証、UTF-8絵文字はJS文字列でぶった切れ。
Published by

DevTools Feed

Ship faster. Build smarter.

Worth sharing?

Get the best Developer Tools stories of the week in your inbox — no noise, no spam.

Originally reported by dev.to

Stay in the loop

The week's most important stories from DevTools Feed, delivered once a week.