luxio avatar

Reverse a string in javascript | Test for a site im working on

luxio

Published: 07 Jul 2017 › Updated: 07 Jul 2017Reverse a string in javascript | Test for a site im working on

Reverse a string in javascript | Test for a site im working on

Reverse a string in javascript

First we'll create a function called revereString

function reverseString(str){

  return reversedString;
}

Inside the reverseString we will create a variable called reversedString and return it.

function reverseString(str){
  var reversedString;
  return reversedString;
}

Now let's set the reversedString variable to this:

function reverseString(str){
  var reversedString = str.split("").reverse().join("");
  return reversedString;
}
  • str.split("") splits the string after every character and puts it into an array.
  • .reverse("") reverses the array outputted by split().
  • .join("") joins every array element into a string.

To test the program run this

var string = "Hello";
var output = reverseString(string);

console.log(output);
// Expected output "olleH"

IMAGE ALT TEXT HERE

Leave Reverse a string in javascript | Test for a site im working on to:

Written by

Read more #javascript posts


Best Posts From luxio

We have not curated any of luxio's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From luxio