reverse each word in a string js code example

Example 1: 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 2: reverse every word in a string javascript

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

Example 3: javascript reverse each string element in array

let noArray_leftOdd = ['0001101', '0011001', '0010011', '0111101'];

console.log(noArray_leftOdd.map(e => e.split("").reverse().join("")));