How to get the last character of a string in JavaScript

2 Answers

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

 



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

 



answered Jan 31, 2021 by avibootz
edited Jun 20, 2022 by avibootz
...