How to Replace Null Values to Empty String from an Array in Javascript?

Sep 24, 2022 . Admin



Hello dev,

This simple article demonstrates of how to replace null values to empty string from an array in javascript. you'll learn how to null values with empty string from an array in javascript. let’s discuss about replace null value to empty string from an array in javascript. I explained simply step by step how to replace null with empty string in array using javascript.

In this example, I am going to explain to you how to replace a null value with an empty string from an array in javascript. We have to write a function that takes in an object with many keys and replaces all false values with a (".."). We will simply iterate over the original object.

So, let's see bellow solution:

Example: 1
index.html
<!DOCTYPE html>
<html>
    <head>
        <title>How to Replace Null Values to Empty String from an Array in Javascript? - MyWebtuts.com</title>
    </head>
    <body>
        <h1>How to Replace Null Values to Empty String from an Array in Javascript? -  MyWebtuts.com</h1>
        
        <script type="text/javascript">
            const biodata = {name:'Praik',lastname:'Thoriya',age:19,hoby:null};
            const swapValue = (boidata) => {
                Object.keys(biodata).forEach(key => {
                    if(!biodata[key]){
                        biodata[key] = "Cricket";
                    }
                });
            };
            swapValue(biodata);

            //Give output to console...
            console.log(biodata);
        </script>
    </body>
</html>
Output:
{name: 'Praik', lastname: 'Thoriya', age: 19, hoby: 'Cricket'}
I hope it will help you...
#Javascript