

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to reverse a string using only one variable in JavaScript
We are required to write a JavaScript program that given any string uses only one extra variable and produces a reversed string −
The program should not declare or use any inbuilt or custom function.
The program should only make use of vanilla JS and basic loops if required.
Example
The code for this will be −
const string = 'abcdef'; let reverse = ''; while (reverse.length !== string.length) { const index = string.length − 1 − reverse.length; reverse += string[index]; }; console.log(reverse);
Output
And the output in the console will be −
fedcba
- Related Questions & Answers
- How to reverse a String using C#?
- Function to reverse a string JavaScript
- How to transform two or more spaces in a string in only one space? JavaScript
- How to Reverse a String using Unix Shell Programming?
- How to reverse a string using lambda expression in Java?
- How to Reverse a String in PL/SQL using C++
- Write program to reverse a String without using reverse() method in Java?
- Counting substrings of a string that contains only one distinct letter in JavaScript
- Reverse only the odd length words - JavaScript
- How to reverse a string in Python?
- How do you reverse a string in place in JavaScript?
- Java program to reverse a string using recursion
- Python Program to Reverse a String Using Recursion
- Java Program to Reverse a String Using Stacks
- Keeping only alphanumerals in a JavaScript string in JavaScript
Advertisements