- 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
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 Articles
- 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 lambda expression in Java?
- How to Reverse a String in PL/SQL using C++
- How to Reverse a String using Unix Shell Programming?
- Write program to reverse a String without using reverse() method in Java?
- How do you reverse a string in place in JavaScript?
- How to reverse a string in Python?
- Java program to reverse a string using recursion
- Java Program to Reverse a String Using Stacks
- Python Program to Reverse a String Using Recursion
- Counting substrings of a string that contains only one distinct letter in JavaScript
- Reverse only the odd length words - JavaScript
- How to quickly reverse a string in C++?

Advertisements