- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Returning reverse array of integers using JavaScript
Problem
We are required to write a JavaScript function that takes in a number and returns an array of elements containing all natural numbers from n upto 1.
Input
const num = 6;
Output
const output = [ 6, 5, 4, 3, 2, 1 ];
Example
Following is the code −
const num = 6; const reverseArray = (num = 1) => { num = Math.round(num); num = Math.abs(num); const res = []; for(let i = num; i > 0; i--){ res.push(i); }; return res; }; console.log(reverseArray(num));
Output
[ 6, 5, 4, 3, 2, 1 ]
- Related Articles
- Inverting signs of integers in an array using JavaScript
- JavaScript Array reverse()
- Returning just greater array in JavaScript
- Reverse sum array JavaScript
- Array reverse() in JavaScript
- Accessing and returning nested array value - JavaScript?
- Returning array of natural numbers between a range in JavaScript
- Returning the first number that equals its index in an array using JavaScript
- Returning only odd number from array in JavaScript
- Returning the nth even number using JavaScript
- Reverse digits of an integer in JavaScript without using array or string methods
- Reverse an array using C#
- Reverse index value sum of array in JavaScript
- Returning the highest value from an array in JavaScript
- Returning array values that are not odd in JavaScript

Advertisements