How to get the last N characters of a string in JavaScript

2 Answers

0 votes
const s = "javascript";
  
const N = 4;
  
const lastN = s.substr(s.length - N);
  
console.log(lastN);
  
  
  
    
      
/*
run:
      
"ript"
      
*/

 



answered Jan 31, 2021 by avibootz
edited Jul 11, 2022 by avibootz
0 votes
const s = "javascript";
  
const N = 4;
  
const lastN = s.slice(s.length - N);
  
console.log(lastN);
  
  
  
    
      
/*
run:
      
"ript"
      
*/

 



answered Jan 31, 2021 by avibootz
edited Jul 11, 2022 by avibootz
...