How to get the last character of a string in TypeScript

2 Answers

0 votes
const s = "typescript";
  
const lastCharacter = s.substr(s.length - 1);
  
console.log(lastCharacter);
  
  
  
    
      
/*
run:
      
"t"
      
*/

 



answered Jun 20, 2022 by avibootz
0 votes
const s = "typescript";
  
const lastCharacter = s.slice(-1);
  
console.log(lastCharacter);
  
  
  
    
      
/*
run:
      
"t"
      
*/

 



answered Jun 20, 2022 by avibootz
...