JavaScript Terminology – What Is a Function That Modifies Its Object Called?

functionsjavascriptterminology

Sorry for the generic question. I have searched all over and found so many threads similar to this, however not one that answers my specific question – perhaps because the term I'm looking for doesn't even exist.

A friend of mine is learning programming, JavaScript specifically, and he asked me why this wasn't working:

var a = "Hello World";
a.replace("Hello", "Goodbye");

console.log(a)  // Logs "Hello World"

The reason is because replace does not modify a, as strings are immutable in JavaSript. Becuase it returns a string, you'd need to do something like…

var a = "Hello World";
a = a.replace("Hello", "Goodbye");

console.log(a);  // Logs "Goodbye World"

However, the alternative is a function like JavaScript's reverse(), as it modifies whatever calls it. For example:

var fruits = ["Apples", "Oranges", "Bananas"];
fruits.reverse();

console.log(fruits)  // ["Bananas", "Oranges", "Apples"]

When my friend asked me why his replace wasn't working, I realized I was reaching for a word that I don't know (as far as I'm aware)…

"You have to set the string to "string dot replace", because the replace function is ________."

You don't need to set an array equal to "array dot reverse", because reverse is ________."

I'm familiar with prototype functions though I don't believe that's the word I'm looking for. Can anyone help me fill in these blanks?

Best Answer

The pair of concepts that you are looking for are mutable/immutable parameters and in-place/returning of results.

In your examples:

You have to set the string to "string dot replace", because the replace function operates on a string which, in python, is immutable so the replace function returns a new string.

For a C/C++ programmer this is more familiar as parameters "passed by value", rather than "passed by reference", which makes them immutable and returning the result.

You don't need to set an array equal to "array dot reverse", because reverse operates on an array, which is mutable, so is able to make changes in-place before returning.

In languages such as C/C++ this is known as parameters "passed by reference" i.e. passing the address which, if unmodified by const, allows the function to change, mutate, the contents of that address altering the results in-place before returning.

Of course it is not unusual to have a function that returns results by both mechanisms, e.g. int SomeFn(int p1, int p2, int *ErrCode) can, potentially return results both in the return value and by modifying the contents of ErrCode.

A 3rd Method

For completeness a 3rd mechanism for returning results is by side-effect or global, i.e. modifying file scope, program wide, shared or environmental values. This is generally considered bad news as, unless very well documented, you can only find out what is being changed by careful reading of the code. In languages such as C/C++ this is all too easy to do by having an outer scope variable with a given name, possibly even in another module, and no masking local scope variable of the same name. In Python, while you can read the values of values in outer scopes, unless outer scope values are explicitly set as available to be modified with the global keyword, attempting to modify an outer scope variable automatically creates a local of the same name.