SoFunction
Updated on 2025-03-10

Notes on JavaScript data type conversion

1. Immutability of strings

After the string is defined, it will occupy memory space all the time, and the memory space (stack) in the Penguin cannot be reassigned.

2. Short circuit operation

||, && binary operators, return the original value of the operand (original data type and original data) participating in the operation,

After the operation is completed, the operand that causes the operation to end is returned.

3. Triple operator

code1? code2: code3;   Unlike if-else:

Return the value of code2 or code3 ----code2, code3   can be replaced by empty {};

Can't write break, continue.


NaN !=NaN,

Any mathematical operation that NaN participates in, the result is NaN

Conditional expressions with NaN participation: Comparison operator >//</<=/==/===     The operation result is false

! ==/! =   The result of the operation is true

<script>
  var a;
  (Boolean(NaN>=4));
  (Boolean(NaN<4));
  (Boolean(NaN=4));
  (Boolean(NaN==4));
  (Boolean(a=4));
  (NaN);
  (a);
  if(NaN==NaN){
    a = "NaN==NaN";
  }
  var b;
  if(NaN!==NaN){
    b = "NaN!=NaN";
  }
  (a+"\n"+b);
</script>

5. JS simple data type conversion---Special case demonstration
Data: 0, "", false, null, undefined, "123abc", etc.

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head lang="en"&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;&lt;/title&gt;
  &lt;style&gt;
    div {
      line-height: 24px;
      margin: 0;
      padding: 0;
    }
    .one {
      width: 920px;
      position: absolute;
      left: 50%;
      top: 50%;
      margin-left: -460px;
      margin-top: -240px;
    }
    .all {
      float: left;
      border: 2px solid #000000;
    }
    .all-top {
      font-size: 20px;
      font-weight: bold;
    }
    .all-bottom {
      line-height: 48px;
      font-size: 16px;
    }
    .details {
      float: left;
      border: 2px solid #000000;
      line-height: 24px;
      margin-left: -2px;
    }
    .details:hover {
      position: relative;
      border: 2px solid #ff0000;
    }
    .line-long {
      border-top: 2px solid #000000;
      height: 0;
      width: 908px;
    }
    .line-short {
      border-top: 2px dashed #000000;
      height: 0;
      width: 742px;
      margin-left: 166px;
    }
  &lt;/style&gt;
  &lt;script&gt;
    ("&lt;div class='one'&gt;");
    function f1() {
      return typeof res[ - 1];
    }
    var arr = [0, "", false, null, undefined, NaN, 6.66, -9, "abc124", "-12.23abc23", "qwer", "s s"];
    ("&lt;div class='all'&gt;&lt;div class='all-top'&gt;" + "&amp;nbsp;&amp;nbsp;Original data and types" + "&lt;/br&gt;" + "Conversion method&amp;nbsp;&amp;nbsp;&lt;/div&gt;" + "&lt;div class='all-bottom'&gt;" + "+" + "&lt;/br&gt;" + "Number()" + "&lt;/br&gt;" + "parseInt()" + "&lt;/br&gt;" + "parseFloat()" + "&lt;/br&gt;" + "\"\"" + "&lt;/br&gt;" + ".toString" + "&lt;/br&gt;" + "String()" + "&lt;/br&gt;" + "!!" + "&lt;/br&gt;" + "Boolean()" + "&lt;/br&gt;" + "&lt;/div&gt;&lt;/div&gt;")
    for (var i = 0; i &lt; ; i++) {
      switch (true) {
        case arr[i] === "":
        {
          var res = ['""'];
          break;
        }
        default :
        {
          var res = [arr[i] + ""];
        }
      }
      res[] = typeof arr[i];
      res[] = +arr[i];
      res[] = f1();
      res[] = Number(arr[i]);
      res[] = f1();
      res[] = parseInt(arr[i]);
      res[] = f1();
      res[] = parseFloat(arr[i]);
      res[] = f1();
      res[] = arr[i] + "";
      res[] = f1();
      if (i == 3 || i == 4) {//null and undefined do not have .toString() methods, resulting in an error        res[] = "Report an error";
        res[] = "Report an error";
      } else {
        res[] = (arr[i]).toString();
        res[] = f1();
      }
      res[] = String(arr[i]);
      res[] = f1();
      res[] = !!arr[i];
      res[] = f1();
      res[] = Boolean(arr[i]);
      res[] = f1();

      var resString = ("&lt;br&gt;");
      ("&lt;div class='details'&gt;" + resString + "&lt;/br&gt;" + "&lt;/div&gt;");
    }
    var j = 22;
    for (var i = 0; i &lt; 9; i++) {
      ("&lt;div class='line-short' style='margin-top:" + j + "px'&gt;&lt;/div&gt;")
      ("&lt;div class='line-long' style='margin-top:" + j + "px'&gt;&lt;/div&gt;")
    }
    ("&lt;/div&gt;");
  &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;

The above is all the content shared by this article. I hope you like it