Replace vs If Else

Edit on GitHub

Setup

function remove1(s) {
  return s.replace(/!$/, '')
}

function remove2(s) {
  return s.endsWith('!') 
    ? s.slice(0, s.length - 1)
    : s
}

Tests

  • Replace

    remove1('h!')
  • If Else

    remove2('!h!')