How to remove all vowels and remove character from string in JavaScript

Create a program to remove all vowels from a string using the JS functions and create a program to remove the first character from the string using the JS function. In this javascript example create a simple web page to design, perform remove vowels and remove the first character from string using HTML, CSS, JS.

1.  Problem statement

In this, we have to remove the vowels (a,e,i,o,u) from the given string using the javascript function. For that create an HTML file and use the JS function that displays the output.

Solution

  • Create HTML file
  • Add heading <h1></h1> tag as Remove vowels from a string and add style for that .
  • Add second heading in bold tag <b></b> as the string .
  • In javascript function, use let to declare limited variables  var alphabets = [ ‘a’, ‘e’, ‘i’, ‘o’, ‘u’,
    ‘A’, ‘E’, ‘I’, ‘O’, ‘U’ ];
  • In for loop the condition is  for(var i = 0; i < str.length; i++) in that use if condition includes() this method returns true if string contains specified value if (!alphabets.includes(str[i])) and return the result res += str[i];
  • Declare string var  str = “Every day may not be good, but there is something good in every day.”;
  • Display output using document.write(“vowels removed string is- “+ remVowel(str.bold()));

How to remove vowels from a string in JavaScript

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Remove vowels from string</title>
</head>
<body style="text-align: center;">

    <h1> Remove vowels from a String </h1>
    <style>
        .h1{
            font-weight: bold;
            color: black;
        }
    </style>

    <b>The string is:<br/>
        Every day may not be good , but there is something good in every day. </b><br/><br/>

<script>
 
function remVowel(str)
{
    var alphabets = [ 'a', 'e', 'i', 'o', 'u',
               		  'A', 'E', 'I', 'O', 'U' ];
    var res = "";
     
    for(var i = 0; i < str.length; i++)
    {
         
        if (!alphabets.includes(str[i]))
        {
            res += str[i];
        }
    }
    return res;
}
var str = "Every day may not be good , but there is something good in every day.";
document.write("vowels removed string is- "+ remVowel(str.bold()));

 
</script>
</body>
</html>

Output

In this way, we learn how to remove vowels from string using JS.

Remove vowels from string using replace() in JS

Problem statement

In this task, create a simple program to remove vowels from string using the replace() method. For that create Html file and use the JS function that displays the result.

Solution 

  • Create Html file
  • Enter heading as  Remove vowels from string in <h2></h2> tag
  • Enter string  var str = [“Royal Enfield”, “TVS”, “Vespa”, “Bajaj pulsar”, “Hero”, “Honda”,”Yamaha”,”Suzuki”]; 
  • Display string document.write(“Original string- “+”</br>”+str+”</br>”); 
  • Use string.map function to create new array with result str = str.map(function (str) 
  • Use replace function to replace vowels from string  return str.replace(/[aeiou]/g, ”); 
  • Display output using document.write(“Modified string -“+”</br>”+str); 

Js code to remove vowels from string using replace()

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title> </title>
</head>
<body style="text-align: center;">
    <h2 style="color: blue;">Remove vowels from string </h2>
<script type="text/javascript">
    var str = ["Royal Enfield", "TVS", "Vespa", "Bajaj pulsar", "Hero", "Honda","Yamaha","Suzuki"];
document.write("Original string- "+"</br>"+str+"</br>");
str = str.map(function (str) {
    return str.replace(/[aeiou]/g, '');
});

document.write("Modified string -"+"</br>"+str);

</script>
</body>
</html>

Output

remvowel

2. Problem statement

In this, we have to remove the first character from the given string using the javascript function. For that create an HTML file and use the JS function that displays the output.

Solution 

  • Create HTML file
  • Add heading <h1></h1> tag as Remove vowels from a string and add style for that .
  • Declare string var  str = “MayGodBlessYou.”;
  • Print string using document.write( str ); 
  • For the output removing the first character from the string use str=str.substring(1); substring return part of the string from a start index to end index.
  • Last use document. write to output document.write(“<br/>”+”<br/>”+ “Removed first character string is :”+ str.bold());

How to remove the first character from the string in JavaScript

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Remove first letter from string</title>
</head>
<body style="text-align: center;">

    <h1> Remove first char from string </h1>
    <style>
        .h1{
            font-weight: bold;
            color: black;
        }
    </style>

<script>
 
 var str = "MayGodBlessYou.";
document.write( str );

str=str.substring(1);
document.write("<br/>"+"<br/>"+ "Removed first character string is :"+ str.bold());

 
</script>
</body>
</html>

Output

In this way, we will learn how to remove the first character from the string using JS.