2
    str = prompt("enter a string");
rev = str.split("").join("").reverse();
console.log(rev);

Guys, I am trying to reverse an array using the above code but I am getting an error stating "Type error .reverse() .split("") and .join("") is not a function.

Please help.

Utkarsh gaur
  • 25
  • 1
  • 5

2 Answers2

7

Javascript strings have no reverse function. You'll have to reverse the array before joining it together into a string:

const str = 'abcde';
const res = str.split('').reverse().join('');
console.log(res);
CertainPerformance
  • 313,535
  • 40
  • 245
  • 254
0

join turns an array into a string, and reverse is not a function of a string. I think what you might have been going for is:

rev = str.split("").reverse().join("");

...which will reverse a string.

kshetline
  • 10,720
  • 4
  • 28
  • 57