Useful string methods

Search, cut, split and clean text.

JavaScript · Useful string methodsBeginner
const s = '  Hello, World  ';

const t = s.trim();                              // 'Hello, World'
t.toUpperCase();  t.toLowerCase();
t.includes('World');                             // true
t.startsWith('Hello');                           // true
t.slice(0, 5);                                   // 'Hello'
t.split(', ');                                   // ['Hello', 'World']
t.replace('World', 'JS');                        // first match only
t.replaceAll('l', 'L');                          // every match
'5'.padStart(3, '0');                            // '005'
'ab'.repeat(3);                                  // 'ababab'
stringincludesslicesplitreplace