Home>

javascript is an untyped language,But at the same time javascript provides a flexible automatic type conversion processing.The basic rule is,If a value of one type is used in an environment that requires another type of value,JavaScript will automatically convert this value to the required type.

1. Immutability of strings

After the string is defined,Will always occupy memory space,Penguin's memory space (stack) cannot be reassigned.

2. Short-circuit operation

||,&&binary operators,Returns the original values ​​(original data type and original data) of the operands involved

After the calculation,Returns the operand that caused the operation to end.

3. Ternary operator

Code1?code2:code3;different from if-else:

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

Can't write break, continue.

4.nan

Nan!=nan,

Any mathematical operation involving nan,The results are all nan

condition Condition expression with nan participation:comparison operator>/>= /</< ====/===

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

<script>
  var a;
  console.log (boolean (nan>= 4));
  console.log (boolean (nan<4));
  console.log (boolean (nan=4));
  console.log (boolean (nan == 4));
  console.log (boolean (a=4));
  console.log (nan);
  console.log (a);
  if (nan == nan) {
    a="nan == nan";
  }
  var b;
  if (nan! == nan) {
    b="nan!=nan";
  }
  console.log (a + "\ n" + b);
</script>

5. js simple data type conversion --- special case demonstration

Data:0, "", false, null, undefined, "123abc", etc.

<! Doctype html>
<html>
<head lang="en">
  <meta charset="utf-8">
  <title></title>
  <style>
    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;
    }
  </style>
  <script>
    document.write ("<div>");
    function f1 () {
      return typeof res [res.length-1];
    }
    var arr=[0, "", false, null, undefined, nan, 6.66, -9, "abc124", "-12.23abc23", "qwer", "s s"];
    document.write ("<div><div>" + "raw data and type" + "</br>" + "conversion method</div>" + "<div>" + "+" + "</br>" + "number ()" + "</br>" + "parseint ()" + "</br>" + "parsefloat ()" + "</br>" + "\" \ "" + "</br>" + ".tostring" + "</br>" + "string ()" + "</br>" + "!!" + "< ;/br>"+" boolean () "+"</br>"+"</div></div>")
    for (var i=0;i<arr.length;i ++) {
      switch (true) {
        case arr [i] === "":
        {
          var res=["" ""];
          break;
        }
        default:
        {
          var res=[arr [i] + ""];
        }
      }
      res [res.length]=typeof arr [i];
      res [res.length]=+ arr [i];
      res [res.length]=f1 ();
      res [res.length]=number (arr [i]);
      res [res.length]=f1 ();
      res [res.length]=parseint (arr [i]);
      res [res.length]=f1 ();
      res [res.length]=parsefloat (arr [i]);
      res [res.length]=f1 ();
      res [res.length]=arr [i] + "";
      res [res.length]=f1 ();
      if (i == 3 || i == 4) {//null and undefined have no .tostring () method, which causes an error
        res [res.length]="Report an error";
        res [res.length]="Report an error";
      } else {
        res [res.length]=(arr [i]). tostring ();
        res [res.length]=f1 ();
      }
      res [res.length]=string (arr [i]);
      res [res.length]=f1 ();
      res [res.length]=!! arr [i];
      res [res.length]=f1 ();
      res [res.length]=boolean (arr [i]);
      res [res.length]=f1 ();
      var resstring=res.join ("<br>");
      document.write ("<div>" + resstring + "</br>" + "</div&";);
    }
    var j=22;
    for (var i=0;i<9;i ++) {
      document.write ("<div + j +" px "></div>")
      document.write ("<div + j +" px "></div>")
    }
    document.write ("</div>");
  </script>
</head>
<body>
</body>
</html>
  • Previous AngularJS basic ng-class-even instruction usage
  • Next Method to prevent mysql from repeatedly inserting records