js check if string ends with code example

Example 1: javascript endswith

"Hello world".endsWith("world");//true
"Hello world".endsWith("Hello");//false

Example 2: javascript check if string ends with

function endsWith(str, suffix) {
    return str.indexOf(suffix, str.length - suffix.length) !== -1;
}

endsWith("hello young man","man");//true
endsWith("hello young man","boy");//false

Example 3: javascript string ends with

const s = 'I am going to become a FULL STACK JS Dev with Coderslang';

console.log(s.endsWith('lang'));             // true
console.log(s.endsWith('LANG'));             // false

Example 4: how to compare a string with its ending in javascript

function solution(str, ending){
  return str.indexOf(ending, str.length - ending.length) !== -1;
}