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

2 Answers

0 votes
const str = 'javascript';

const first = str.charAt(0);
console.log(first); 

const last = str.charAt(str.length - 1);
console.log(last); 

 
 

 
/*
run:

"j"
"t"

*/

 



answered Feb 10, 2022 by avibootz
0 votes
const str = 'javascript';

const first = str[0];
console.log(first); 

const last = str[str.length - 1];
console.log(last); 

 
 

 
/*
run:

"j"
"t"

*/

 



answered Feb 10, 2022 by avibootz
...