1

I was recently working on a Leetcode question and stumbled across something that I at first thought was strange, but then after looking at it for a second, realized it might have to do with JavaScript String immutability.

I wanted to ask and see what the community thought.

Here's a snippet of some code:

var keys = function(words) {

  for (var i = 0; i < words.length; i++) {
    // With lower, ['hello', 'alaska', 'dad', 'peace']
    var lower = words[i].toLowerCase();
    console.log(`Word is ${lower}`);
  }

}

keys(['Hello', 'Alaska', 'Dad', 'Peace']);

Does lower work because since JS strings are immutable, we'd have to make a copy of the string first in order to use it elsewhere with the applied transformation (making the string lowercase)?

var keys = function(words) {

  for (var i = 0; i < words.length; i++) {
    // Without lower, ['Hello', 'Alaska', 'Dad', 'Peace']
    words[i].toLowerCase();
    console.log(`Word is ${words[i]}`);
  }

}

keys(['Hello', 'Alaska', 'Dad', 'Peace']);

Does this not work because again, strings are immutable so a transformation cannot be applied?

Thanks

12
  • "we'd have to make a copy of the string first in order to use it elsewhere with the applied transformation" toLowerCase() simply returns a new string. You don't have to make a "copy" of the original string. Besides, since strings are immutable, doing a copy is a) not necessary and b) you wouldn't be able to tell whether you actually have a copy or not. Commented Jun 7, 2018 at 0:07
  • 1
    I don't understand the question. What's the difference between the first lower and second lower? Commented Jun 7, 2018 at 0:13
  • 1
    Still don't understand, all you did was remove the second var declaration, which is redundant because the variable is already declared. Commented Jun 7, 2018 at 0:17
  • 1
    It logs the same thing in both places. Commented Jun 7, 2018 at 0:17
  • 1
    Have you noticed how dad's are special, even when forced to be lowercase they come out like -> 'Dad' :) Commented Jun 7, 2018 at 0:30

1 Answer 1

3

toLowerCase() doesn't modify the string it's called on. Since strings are immutable, it couldn't do so. But even if they were mutable, it's just not what the function is defined to do. It returns a new string containing the lowercase version of the original string.

It's analogous to the slice() method of arrays. Arrays are mutable, but slice() returns a new array containing the specified sub-array, it doesn't modify the original array (that's what splice() does).

So if you want to do anything with that new string, you need to assign it somewhere, pass it as an argument to a function, or call some other method on the result.

If you don't want the lower variable, you can write:

console.log(`Row is ${row[i].toLowerCase()}`);
Sign up to request clarification or add additional context in comments.

2 Comments

@Keith I'm sure it's the old "downvote answers to poor questions" reason.
I also thought the OP made a good stab at asking too, if he doesn't understand something, isn't that why people are here to help :( Anyway, I'm going to counter vote now. :).

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.