I tried the below code in JavaScript:
console.log(str.toLowerCase().trim().replace(str[0], str[0].toUpperCase()));
Case 1 - str = 'variable':
const str = 'variable';
console.log(str.toLowerCase().trim().replace(str[0], str[0].toUpperCase()));
It gives the expected output which is, 'Variable', i.e, the first letter of the string is in uppercase.
Case 2 - str = 'Variable':
const str = 'Variable';
console.log(str.toLowerCase().trim().replace(str[0], str[0].toUpperCase()));
It gives the a strange output- 'variable'.
I am quite new to JavaScript and am unable to understand what the reason is for this behavior.
const str = 'Variable';
console.log(str.toLowerCase().trim().replace(str[0], str[0].toUpperCase()));