Sep 21, 2020

JavaScript String Immutability with Example and Output

In Javascript, Strings are Immutable which means their value cannot be changed over time. Example below:

 //String Immutability

var strs = new String("Test");
console.log(strs); // [String: 'Test']
var strs1 = "Test";
console.log(strs1); //Test

console.log(strs); // [String: 'Test']
console.log(strs1); //Test

To perform any changes we need to use strings as in example below:

//To perform any changes
strs = strs.concat("1234");
console.log(strs); //Test1234
strs1 = strs1.concat("1234");
console.log(strs1); //Test1234

We'll have a look at few String methods in my next post.

No comments:

Post a Comment

Thanks a lot for your valuable Comment!

Featured this week

Latest Employment BUZZ: 4 working days in a week?

COVID-19 has hit mostly all sectors including IT  too bad. If we think otherwise, on the other hand,  there have been many new technical inn...