window.ENTITIES={'/api/snippets/javascript/javascript%20-%20test%20if%20string%20has%20postfix%20(suffix)':[{"result":true,"message":null,"batch":{"type":"javascript","name":"javascript - test if string has postfix (suffix)","items":[{"id":"jo6Kxp","type":"javascript","name":"JavaScript - test if string has postfix (suffix)","content":"// ONLINE-RUNNER:browser;\n\nconst hasPostfix = (text, prefix) => {\n if (prefix.length > text.length) {\n return false;\n }\n for (let i = prefix.length - 1, j = text.length - 1; i >= 0; --i, --j) {\n if (text[j] !== prefix[i]) {\n return false;\n }\n }\n return true;\n};\n\n\n// Usage example:\n\nconst text = 'This is example text.';\n\nconsole.log(hasPostfix(text, '')); // true\nconsole.log(hasPostfix(text, 'text.')); // true\nconsole.log(hasPostfix(text, 'example text.')); // true\n\nconsole.log(hasPostfix(text, '123')); // false\nconsole.log(hasPostfix(text, ' ')); // false","source":"","author":{"id":"V08Rqa","name":"Geospatial-Palus","avatar":"1629139874698__V08Rqa__w40px_h40px.jpg","points":660,"role":"BASIC"},"creationTime":1668161526000,"updateTime":1710984050000,"removalTime":null},{"id":"1GMErp","type":"javascript","name":"JavaScript - test if string has postfix (suffix)","content":"// ONLINE-RUNNER:browser;\n\n// Note: ES6 (ES2015) introduced String endsWith() method that lets to check if string starts with prefix.\n\n\nconst text = 'This is example text.';\n\nconsole.log(text.endsWith('')); // true\nconsole.log(text.endsWith('text.')); // true\nconsole.log(text.endsWith('example text.')); // true\n\nconsole.log(text.endsWith('123')); // false\nconsole.log(text.endsWith(' ')); // false","source":"","author":{"id":"9oAmNo","name":"Selina-Miranda","avatar":"1629030448981__9oAmNo__w40px_h40px.png","points":737,"role":"BASIC"},"creationTime":1668161598000,"updateTime":1710984054000,"removalTime":null}]}}]};