I have a virtual keyboard and in my function to check if the caps lock is on, I also want to check if a letter key following sentence-ending punctuation: ., ?, or !
If so, then I want to use toUpperCase to capitalize the next letter. Here is the code just for that section of the function:
Just as regards the original question about making the if condition shorter:
The first thing that jumps out is that instead of doing the lookup punc.charAt(len - 1) three times, you could put that in a variable. You might also consider looking at .includes().
Is there a reason you use String.charAt() instead of simply string[index]?
Another way to put your condition could be:
let lastChar=punc[len-1]
if (/[?!.]/.test(lastChar)) { ... }
Regex is prolly the best way to manipulate string. You could even go without the condition statement in your code and directly look for punctuation marks in a string and uppercase following letters
edit: had swapped the test parameter and the object its applied on
Regular expressions would be my go-to, especially if you want to do something like check for all non-alphanumeric characters. I know that a lot of people get frustrated by them though.