How to create an array containing 1 to N in JavaScript

2 Answers

0 votes
const N = 10
  
let arr = new Array(N); 
  
for (let i = 0; i < arr.length; i++) {
    arr[i] = i + 1;
}
  
console.log(arr);
    
    
    
      
      
/*
run:
      
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      
*/
    

 



answered Jan 28, 2021 by avibootz
edited Aug 23, 2023 by avibootz
0 votes
const N = 10
 
const arr = Array.from(
    {length: N},
    (_, index) => index + 1
);
 
console.log(arr);
   
   
     
     
/*
run:
     
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
     
*/

 



answered Jul 7, 2022 by avibootz
...