3
    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
  • 35
  • 1
  • 5

2 Answers2

9

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
  • 356,069
  • 52
  • 309
  • 320
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
  • 12,547
  • 4
  • 37
  • 73