.replace function

In this code

function titleCase(str) {
  return str.toLowerCase().replace(/(^|\s)\S/g, L => L.toUpperCase());

Why couldnt we do

function titleCase(str) {
  return str.toLowerCase().replace(/(^|\s)\S/g, L.toUpperCase());


function titleCase(str) {
  return str.toLowerCase().replace(/(^|\s)\S/g, return L.toUpperCase());

Why is the function needed there as the value to replace the search value with?



In the second one , as toUpperCase() is a function, you can call a function with an arrow “=>”, so if the arrows isn’t there, the code can’t call the function.

(i’m not a pro just remember about arrow functions)

Hello there,

What is L referring to:

return str.toLowerCase().replace(/(^|\s)\S/g, L.toUpperCase());
1 Like

I thought that L was referring to each character that fits the regex, And then you use toUpperCase() to make each “L” capital.

Because in this code

function titleCase(str) {
  return str.toLowerCase().replace(/(^|\s)\S/g, L => L.toUpperCase());

L is referring to each character that satisfies the Regex right?

So why couldnt we just do

return str.toLowerCase().replace(/(^|\s)\S/g, L.toUpperCase());

Which is saying make each L or character that satisfies the regex, capital.

What I am wondering is why you need to replace the satisfied Regex with a function, why cant you just say L.toUpperCase.

Yea, but this code

function titleCase(str) {
  return str.toLowerCase().replace(/(^|\s)\S/g, L => L.toUpperCase());

Is equal to this code

function titleCase(str) {
  return str.toLowerCase().replace(/(^|\s)\S/g, function(L) => L.toUpperCase());

I doesnt make sense that you are returning a function to a function

careful there, this is wrong syntax, maybe you wanted to write function(L) {return L.toUpperCase();}

the thing is that L is defined because it is a function parameter, remove the function and L is not defined and you can’t use it
also, the replace method accepts a string as second argument or a callback function, L.toUpperCase() is neither, it’s just an expression with a method used on a never used before variable

Ok, so

L => L.toUpperCase()

is a callback function? If so then why is it a calback function? I thought that a callback function was function x (function y)

Also L refers to all characters that match the Regex passed in the first parameter right?


the replace method pass to the callback function each match, so yes in this case the L parameter refers to each character matched

with different regular expressions the matched strings could be words, instead of single characters, or any other part of the string on which replace is used

callback is the term used for functions that are arguments of other functions/methods, so when a method expect a function as argument we say that it takes a callback.
Other methods that take a callback are for example map and filter

1 Like

One last thing, why here:

does the L need a function to be defined?

you can give any name to function parameters
for the function L => L.toUpperCase() it was chosen to use L.
It works anyway if you change the name of the parameter, but for short callbacks written with arrow function it is usually used a single letter.
For example if we want to be descriptive in naming the parameters it could have been:
firstLetterOfWord => firstLetterOfWord.toUpperCase()

Yes but why does L need to be passed to a function to have a value.
Why cant it be


Where L has a value too. Is it only because replace only accepts callback functions?

and where have you defined L?

if you do like that L is not defined, it doesn’t have a value, it was never declared before

So L is being defined here same as f(L)??

L was never before declared as a let, const, or var here

it is a function parameter of the function L => L.toUpperCase(), so it is declared as a function parameter

you never declare a function parameter with let, var or const even with the other function syntax: function(L) {return L.toUpperCase();}