1

I know that String and Number objects are immutable but are all built-in objects immutable as well? If it's not true, can you give me an example of a built-in object that is mutable?

4
  • No, you can attach and delete properties to many of the built-in objects. Although, the prototypes of the constructor built-ins are non-configurable, non-enumerable, non-writable. Commented Oct 4, 2015 at 18:05
  • @MinusFour are you referring for example to String-objects? Can we change the properties of them? Commented Oct 4, 2015 at 18:10
  • That would be an instance of a String wrapped around an object, so that's not a built-in. Built-ins would be Array, Object, Number, String and their respective prototypes. Commented Oct 4, 2015 at 18:14
  • Wait, but so far as I know a String-object is for example var x = new String("John"). This is an immutable object because we can't modify anything of it. For example it's content, that's why we cannot make x.substr(1). That will not change the content of x. Commented Oct 4, 2015 at 18:19

2 Answers 2

3

Arrays are mutable. Here are a list of mutable methods for arrays.

push - Elements are added on to the end of the array

pop - Elements are removed from the end of the array

shift - Elements are removed from the beginning of the array

unshift - Elements are added to the beginning of the array

splice - Adds/removes elements to an array

None of these methods create a new array, but modify the existing array.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the detailed answer :)
2

Nope, for instance an array is mutable. If you have an array of strings and you push a string to it, you don't get a new array with an extra string added to it. The original array is altered.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.