0

As i heard, String is immutable in JavaScript then how splice method changing the original String?

As I know var name = 'Alto';// it will create an new object in global space memory with 'alto' and assign to name reference.

name = 'swift'; // it will create an new object in global space memory and with 'swift' assign to name reference. ('Alto' string object will be moved to garbage collector whenever possible).

var newName = name.slice(0,2);// here splicing the 'sw' from 'swift' create an new Object in global space and assign to newName. name will have 'swift' since string is immutable.

var newName1 = name.splice(0,2);// here cut the 'sw' from 'swift'(original string) and assign to newName. now name will have 'ift'. so how String is an immutable?

Can anyone explain the process behind splice?

albert Jegani
  • 462
  • 5
  • 15

1 Answers1

0

They are immutable. You cannot change a character within a string with something like var myString = "abbdef"; myString[2] = 'c'. The string manipulation methods such as trim, slice return new strings.

Vikas Suryawanshi
  • 522
  • 1
  • 5
  • 12