Removing all non-alphabetic characters from a string in JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

We are required to write a JavaScript function that takes in a string of characters. The function should construct a new string in which all the non-alphabetic characters from the original string are removed and return that string. If the string contains spaces, then it should not be removed.

For example −

If the input string is −

const str = 'he@656llo wor?ld';

Then the output string should be −

const str = 'he@656llo wor?ld';


Following is the code −

const str = 'he@656llo wor?ld';
const isAlphaOrSpace = char => ((char.toLowerCase() !==
char.toUpperCase()) || char === ' ');
const removeSpecials = (str = '') => {
   let res = '';
   const { length: len } = str;
   for(let i = 0; i < len; i++){
      const el = str[i];
         res += el;
   return res;


Following is the output on console −

hello world
Published on 11-Dec-2020 14:08:56