How to get the first and last characters of a string in TypeScript

2 Answers

0 votes
const str = 'typescript programming';
 
const first = str.charAt(0);
console.log(first); 
 
const last = str.charAt(str.length - 1);
console.log(last); 
 
  
  
 
  
/*
run:
 
"t"
"g"
 
*/

 



answered Feb 10, 2022 by avibootz
0 votes
const str = 'typescript programming';
 
const first = str[0];
console.log(first); 
 
const last = str[str.length - 1];
console.log(last); 
 
  
  
 
  
/*
run:
 
"t"
"g"
 
*/

 



answered Feb 10, 2022 by avibootz
...