How to get first N characters of a string in JavaScript

2 Answers

0 votes
const s = "javascript";
    
const N = 4;
    
const firstN = s.slice(0, N)
    
console.log(firstN);
    
    
  
      
        
/*
run:
        
"java"
        
*/

 



answered Feb 7, 2021 by avibootz
edited Jul 11, 2022 by avibootz
0 votes
const s = "javascript";
    
const N = 5;
    
const firstN = s.substring(0, N)
    
console.log(firstN);
    
    
  
      
        
/*
run:
        
javas
        
*/

 



answered Feb 7, 2021 by avibootz
edited Jul 11, 2022 by avibootz
...