js string method replace () and toUpperCase () whether it will change the original string problem

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

Foreword

js built-in to a string number of properties and methods, properties such as length, very practical.
So the question is, we know that some built-in string is to "change" the string will "look" and return a new string.
It will not affect the original string it? The answer is no!

prove

var demo = "hello,world";
var NEW = demo.toUpperCase();

console.log(NEW); // 输出:HELLO,WORLD
console.log(demo);// 输出:hello,world

Can be seen, the use of toUpperCase () method, "hello, world" becomes "HELLO, WORLD". If you assume that will change the original strings, it should be all uppercase demo at this time, but still the original string initially defined, which is not enough to explain affect the original string.

In js string is fixed, similar to replace () and toUpperCase () method returns a new string, but the original string itself has not changed.

Guess you like

Origin blog.csdn.net/weixin_44198965/article/details/94189332