reverse a word in js code example

Example 1: javascript reverse a string

function reverseString(s){
    return s.split("").reverse().join("");
}
reverseString("Hello");//"olleH"

Example 2: reverse every word in a string javascript

// hey buddy, here are different implementations, choose your favourite !!

// Examples
// "This is an example!" ==> "sihT si na !elpmaxe"
// "double  spaces"      ==> "elbuod  secaps"

function reverseWords(str) {
  const allWords = str.split(" ")
  return allWords.map(item => item.split("").reverse().join("")).join(" ")
}


function reverseWords(str) {
  return str
    .split(" ")
    .map(word => {
      return word.split("").reverse().join("")
    })
    .join(" ")
}


var reverseWords = s => s.replace(/\S+/g, v => [...v].reverse().join``)

Example 3: reverse words in a string javascript

function reverseString(str) {
    return str.split("").reverse().join("");
}
reverseString("hello");

Example 4: reverse every word in a string javascript

function reverseWords(str) {
  const allWords = str.split(" ")
  return allWords.map(item => item.split("").reverse().join("")).join(" ")
}

Example 5: reverse words javascript

function reverse (word) {
    word = word.split('.').reverse().join('.')
    return word

}
word = 'i.like.this.program.very.much'
word = 'pqr.mno'
console.log(reverse(word))

Tags:

Java Example