-1

I have a very simple code that converts every letter to lower case and later sets upper case for every first letter using RegExp:

let quote = 'I dO nOT lIke gREen eGgS anD HAM';
let fixQuote = quote.toLocaleLowerCase('en-US');

let regex = /(^\w|\s\w)/g;

let fixedQuote = fixQuote.replace(regex, m => m.toUpperCase());

Can someone explain what does the arrow function m => m. does in this part? I don't understand what m stands for here.

Thanks!

marcin2x4
  • 1,321
  • 2
  • 18
  • 44

2 Answers2

3

It is simply the internal variable of an anonymous function:

  • m is the parameter variable with value of regex.
  • m.toUpperCase() is the return value.

Essentially the same as:

function toUpperCase(m){
    return m.toUpperCase();
}
Dcdanny
  • 469
  • 2
  • 8
2

In this case m is just a parameter to a function, so a variable name. You can imagine that function written like this:

function toUpperCase(m) {
   return m.toUpperCase();
}

Every match found by your regex will be passed to the function as an argument. The variable m will hold that value.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192