function ensureEndsWithDot(string) { return string.slice(-1) == '.' ? string : string + '.'; } alert(ensureEndsWithDot('abc')); alert(ensureEndsWithDot('abc.'));