9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Javascriptのreplaceは破壊系ではない。

9
Posted at

小さなことですが無駄にハマったのでメモ程度に。

JavascriptのStringオブジェクトについてるreplaceメソッドは、
破壊系ではないので何かに代入してあげないと意味がないです。

つまり、

var hoge = "hoge";
console.log(hoge); // output: hoge

hoge.replace("hoge", "fuga");

console.log(hoge); // output: hoge

になっちゃいますね。
最終行で出力 "fuga" を期待したいなら、

var hoge = "hoge";
console.log(hoge); // output: hoge

var fuga = hoge.replace("hoge", "fuga");

console.log(fuga); // output: fuga

という風にしましょう。

9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?