title: Replace NaN with another character using JavaScript
body: <p>Can you please tell me how to replace <code>NaN</code> with another character? There are variables that are assigned some numeric value.
  Further in the function, these variables get their values, which I then display in the html <code>tabl</code>e. But sometimes some variable returns  <code>NaN</code>.</p>
  <p>To fix this, I separately created an array with variables that already have some values. Next, I started the loop. If the condition returns <code>NaN</code>, then it should be replaced with <code>&quot;-&quot;</code>. But it doesn't work.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="true">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var a, b, c, d, x1, x2, x3, x4;
  var elem = document.getElementById.bind(document);
  
  function iFunc() {
    a = 1;
    b = 3;
    c = 2;
    d = NaN;
  
    x1 = elem('a1').innerHTML = a;
    x2 = elem('b1').innerHTML = b;
    x3 = elem('c1').innerHTML = c;
    x4 = elem('d1').innerHTML = d;
  
    var arrX = [x1, x2, x3, x4];
  
    for (var x of arrX) {
      if (x !== x) {
        x = "-"; // the character that was supposed to replace NaN
        console.log(x);
      }
    }
  }
  
  iFunc();</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;table&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;span id="a1"&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span id="b1"&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span id="c1"&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span id="d1"&gt;&lt;/span&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;</code></pre>
  </div>
  </div>
  </p>
  <p><strong>UPD:</strong> with .map()</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="true">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var a, b, c, d, x1, x2, x3, x4;
  var elem = document.getElementById.bind(document);
  
  function iFunc() {
    a = 1;
    b = 3;
    c = 2;
    d = NaN;
  
    x1 = elem('a1').innerHTML = a;
    x2 = elem('b1').innerHTML = b;
    x3 = elem('c1').innerHTML = c;
    x4 = elem('d1').innerHTML = d;
  
    var arrX = [x1, x2, x3, x4].map(n =&gt; n != n ? "-" : n);
  
    console.log(arrX);
  }
  
  iFunc();</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;table&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;span id="a1"&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span id="b1"&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span id="c1"&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td&gt;&lt;span id="d1"&gt;&lt;/span&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;</code></pre>
  </div>
  </div>
  </p>