JavaScript endsWith() to Check a String Suffix
Use text.endsWith(suffix) to check whether a JavaScript string ends with a specific sequence. It returns true for a match and false otherwise. Pass endPosition as the second argument to check the end of an initial portion of the string.
Check for an exact suffix
console.log('report.pdf'.endsWith('.pdf')); // true
console.log('report.PDF'.endsWith('.pdf')); // false
console.log('report.pdf '.endsWith('.pdf')); // false
console.log('abc'.endsWith('')); // true
try {
'abc'.endsWith(/c/);
} catch (error) {
console.log(error.name); // TypeError
}
Case and spaces both matter. An empty suffix always matches, so decide how to handle blank input before calling the method when a suffix is required. Passing a regular expression object throws TypeError. Use RegExp.test() for a pattern-based check. See ECMAScript · String.prototype.endsWith() for the matching rules.
Limit the comparison with endPosition
endPosition is the length of the initial portion being checked. A value of 3 includes indices 0, 1, and 2. The method converts the value to an integer and clamps it between 0 and text.length. Omitting the argument uses the full length.
const text = 'abzcdz';
console.log(text.endsWith('z')); // true
console.log(text.endsWith('z', 2)); // false
console.log(text.endsWith('z', 3)); // true
console.log(text.slice(0, 3)); // 'abz'
// endPosition
console.log(text.endsWith('z', 3.9)); // true
console.log(text.endsWith('z', -1)); // false
console.log(text.endsWith('z', 99)); // true
console.log('A🙂Z'.endsWith('🙂', 3)); // true
console.log('A🙂Z'.endsWith('🙂', 2)); // false
The boundary counts UTF-16 code units. In 'A🙂Z', the emoji occupies two units, so 3 includes 'A🙂' while 2 cuts the emoji in half. If your interface accepts a count of visible characters, convert it to a code-unit boundary before passing it in.
Check filenames and URL paths
Decide which part of the input you need to compare. For a URL path, use new URL(address).pathname to exclude the query and fragment. For a relative address, supply a base as in new URL(path, base). URL Standard · URL.pathname
console.log('report.pdf'.endsWith('.pdf')); // true
console.log('report.pdf?download=1'.endsWith('.pdf')); // false
const url = new URL('/report.pdf?download=1#preview', 'https://example.com');
console.log(url.pathname.endsWith('.pdf')); // true
console.log('REPORT.PDF'.toLowerCase().endsWith('.pdf')); // true
If your service treats ASCII filename extensions as case-insensitive, lowercase the filename before checking for .pdf. A matching suffix tells you how the file is named. Validate the actual file format and enforce upload rules on the server.
Try the comparison in a standalone page
Save the code below as string-ends-with.html using UTF-8 and open it in a browser. With abzcdz and z, the result starts at true. Enable the end position and leave it at 3 to check abz, which still matches. Change it to 2 to check ab and get false.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>String suffix check</title>
</head>
<body>
<label for="text">Original string</label>
<input id="text" type="text" value="abzcdz">
<label for="suffix">Suffix</label>
<input id="suffix" type="text" value="z">
<label><input id="use-end" type="checkbox"> Use end position</label>
<label for="end">End position</label>
<input id="end" type="number" min="0" step="1" value="3" disabled>
<pre id="output" aria-live="polite"></pre>
<script>
const text = document.querySelector('#text');
const suffix = document.querySelector('#suffix');
const useEnd = document.querySelector('#use-end');
const end = document.querySelector('#end');
const output = document.querySelector('#output');
function show() {
const value = text.value;
const target = suffix.value;
end.disabled = !useEnd.checked;
const position = Number(end.value);
if (useEnd.checked && (end.value.trim() === '' ||
!Number.isInteger(position) || position < 0)) {
output.textContent = "Enter a non-negative integer for the end position.";
return;
}
const checked = useEnd.checked ? value.slice(0, position) : value;
const result = useEnd.checked
? value.endsWith(target, position) : value.endsWith(target);
output.textContent = `Checked text: ${JSON.stringify(checked)}\n` +
`Requested suffix: ${JSON.stringify(target)}\n` +
`endsWith() result: ${result}`;
}
text.addEventListener('input', show);
suffix.addEventListener('input', show);
useEnd.addEventListener('change', show);
end.addEventListener('input', show);
show();
</script>
</body>
</html>
Keep the position enabled and clear its value to see the input message. Turn it off to check the entire string again. The demo rejects negative and fractional positions in its input. The method itself converts them to an integer and clamps the value. Clear the suffix to observe the empty match.




