The endsWith() method can be called from any String variable.
- string can be any string value or variable
- SEARCH should be the string which you want to check if string ends with
- LENGTH is OPTIONAL
- If specified, LENGTH will be used as the length of the STRING being checked
- If it is less than the actual length of string, the rest of the string beyond LENGTH will be ignored
- This can be useful if you want to check whether a string exists at a certain position within a string
- A boolean value of TRUE (if the string ends with the given value) or FALSE is returned
The below examples show how the endsWith method is used. A string variable named test is declared, and the endswith() method is called on it.
let test = 'Beam me up, Scotty.' test.endsWith('Scotty.'); // true test.endsWith('up'); // false test.endsWith('up', 10); // true
Note the last example supplies a length, so that only a portion of the string is used.