Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Semrush - keyword research tool

Linux Foundation Training and Certification

Teach Your Child To Read

Disclosure: My content contains affiliate links.

32,304 questions

42,479 answers

573 users

How to reverse a string in JavaScript

6 Answers

0 votes
function ReverseString(s) {
    let rev = '';
   
    for (let i = s.length - 1; i >= 0; i--)
        rev += s[i];
         
    return rev;
}

let s = "JavaScript C C++ C# Java";
 
console.log(ReverseString(s));
 

 
/*
run:  
 
avaJ #C ++C C tpircSavaJ
 
*/

 



Learn & Practice Python
with the most comprehensive set of 13 hands-on online Python courses
Start now


answered May 19, 2017 by avibootz
edited Apr 26 by avibootz
0 votes
function ReverseString(s) {
    let rev = [];
    
    for (let i = 0, len = s.length; i <= len; i++)
        rev.push(s.charAt(len - i));
    
    return rev.join('');
}

let s = "JavaScript C C++ C# Java";
 
console.log(ReverseString(s));
 

 
/*
run:  
 
avaJ #C ++C C tpircSavaJ
 
*/

 



Learn & Practice Python
with the most comprehensive set of 13 hands-on online Python courses
Start now


answered May 19, 2017 by avibootz
edited Apr 26 by avibootz
0 votes
function ReverseString(s) {
    return s.split('').reverse().join('');
}

let s = "JavaScript C C++ C# Java";
 
console.log(ReverseString(s));
 

 
/*
run:  
 
avaJ #C ++C C tpircSavaJ
 
*/

 



Learn & Practice Python
with the most comprehensive set of 13 hands-on online Python courses
Start now


answered May 19, 2017 by avibootz
edited Apr 26 by avibootz
0 votes
function ReverseString(s) {
    for (let i = s.length - 1, rev = ''; i >= 0; rev += s[i--]) { }
    
    return rev;
}

let s = "JavaScript C C++ C# Java";
 
console.log(ReverseString(s));
 

 
/*
run:  
 
avaJ #C ++C C tpircSavaJ
 
*/

 



Learn & Practice Python
with the most comprehensive set of 13 hands-on online Python courses
Start now


answered May 19, 2017 by avibootz
edited Apr 26 by avibootz
0 votes
function ReverseString(s) {
    s = s.split('');
    let len = s.length;
    let middle = Math.floor(len / 2) - 1;
     
    for (let i = 0; i <= middle; i++) {
        let tmp = s[len - i - 1];
        s[len - i - 1] = s[i];
        s[i] = tmp;
    }
    
    return s.join('');
}

let s = "JavaScript C C++ C# Java";
 
console.log(ReverseString(s));
 

 
/*
run:  
 
avaJ #C ++C C tpircSavaJ
 
*/

 



Learn & Practice Python
with the most comprehensive set of 13 hands-on online Python courses
Start now


answered May 19, 2017 by avibootz
edited Apr 26 by avibootz
0 votes
function ReverseString(s) {
  return [...s].reverse().join('');
}
 
let s = "JavaScript C C++ C# Java";
  
console.log(ReverseString(s));
  
 
  
/*
run:  
  
avaJ #C ++C C tpircSavaJ
  
*/
 

 



Learn & Practice Python
with the most comprehensive set of 13 hands-on online Python courses
Start now


answered Sep 2 by avibootz

Related questions

...