Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reversing a string using for loop in JavaScript
We are required to write a JavaScript function that takes in a string as the only argument. The function should construct a new reversed string based on the input string using the for a loop.
Example
Following is the code −
const str = 'this is the original string';
const reverseString = (str = '') => {
let reverse = '';
const { length: len } = str;
for(let i = len - 1; i >= 0; i--){
reverse += str[i];
};
return reverse;
};
console.log(reverseString(str));
Output
Following is the output on console −
gnirts lanigiro eht si siht
Advertisements