Search notes:

Javascript code snippets

executable-code_execution-contexts/arguments.html

<!DOCTYPE html>
<html>
<head>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  <title>JavaScript: arguments</title>

  <script type="text/javascript">

    var out;

    function with_params(a1, a2, a3) {
      out.innerHTML += 'length: ' + arguments.length + '<br>';
    }

    function without_params() {
      out.innerHTML += 'length: ' + arguments.length + '<br>';
    }

    function fooObj() {
      
    }

    fooObj.prototype.bar = function () {
      out.innerHTML += 'length: ' + arguments.length + '<br>';
    }

    
    function main() {

      out = document.getElementById('out');

      with_params   (1, 2, 3   );        // length: 3
      with_params   (1         );        // length: 1
      with_params   (          );        // length: 0
      with_params   (1, 2, 3, 4);        // length: 4

      without_params(1, 2, 3   );        // length: 3

      var wo  = new without_params(1);   // length: 1

      without_params.prototype.x = with_params;;

      wo.x(2,3);                         // length: 2

      var foo = new fooObj();
      foo.bar(1, 2, 3, 4, 5);            // length: 5

    }

  </script>

</head>
<body onload='main()';>

  <div id="out"></div>

</body>
</html>

Github repository about-javascript, path: /executable-code_execution-contexts/arguments.html

keywords/const.html

<!DOCTYPE html>
<html>
<head>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  <title>Javascript: this</title>
  <script type="text/javascript">

    function print(text) {
      out.innerHTML += text + "<br>";
    }

    function main() {

      try {
        const foo = 42;
        print("foo = " + foo);
        foo = 1;
        print("foo = " + foo);

      }
      catch (e) {
        print("exception: " + e);
     // exception: TypeError: invalid assignment to const `foo'
      }
    }

  </script>
</head>
<body onload='main();'>

  <div id='out'>
  </div>

</body>
</html>
Github repository about-javascript, path: /keywords/const.html

keywords/this.html

<!DOCTYPE html>
<html>
<head>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  <title>Javascript: this</title>
  <script type="text/javascript">

    var out;

    function print(text) {
      out.innerHTML += text + "<br>";
    }

    function main() {
      
      out = document.getElementById('out');

      print(this); // [object Window]
      
      var obj = {a: 'aha'};

      obj.f = function() { print(this.a); };

      obj.f(); // aha

      document.getElementById('click-too').onclick = function() {
        print(this); // [object HTMLDivElement]
      }

    }

    function clicked() {
      print(this);
    }

  </script>
</head>
<body onload='main();'>

  <!-- In the following line, «this» refers to the div element, in the called «clicked» function, this is the «window» object -->
  <div onclick='print(this); this.innerHTML = "clicked"; clicked();' style='background-color:yellow'><b>Click me</b></div><br>
  <div id='click-too'                                                style='background-color:pink'  ><b>Click me, too</b></div><br>


  <div id='out'>
  </div>

</body>
</html>
Github repository about-javascript, path: /keywords/this.html

source-text/line-terminator.html

<!DOCTYPE html>
<html>
<head>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  <title>Javascript: Line Terminators</title>
  <script type="text/javascript">

    var out;

    function main() {
      
      text = document.getElementById('text');
      out  = document.getElementById('out');

      var source = "x++\u000Ax++\u000Dx++\u2028x++\u2029x++";

      increment_x = new Function("", source);

      x = 0;
      increment_x();

      text.innerHTML = source;

      out.innerHTML  = "x = " + x;

    }

  </script>
</head>
<body onload='main();'>

  <pre id='text'></pre>
  <pre id='out'></pre>

</body>
</html>
Github repository about-javascript, path: /source-text/line-terminator.html

types/Boolean/true_vs_false.html

<!DOCTYPE html>
<html>
<head>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  <title>true vs false</title>

  <script type="text/javascript">
    
    var table_, tbody_;

    function true_or_false(x) {


      var tr      = document.createElement('tr');
      var td_str  = document.createElement('td');
      var td_eq   = document.createElement('td');
      var td_eval = document.createElement('td');

      td_str .appendChild(document.createTextNode(x));
      td_eq  .appendChild(document.createTextNode(' is ' ));
      td_eval.appendChild(document.createTextNode(eval('Boolean(' + x + ')' )));

      tr.appendChild(td_str );
      tr.appendChild(td_eq  );
      tr.appendChild(td_eval);

      tbody_.appendChild(tr);

    }
    
    function main() {

      table_ = document.createElement('table');
      tbody_ = document.createElement('tbody');

      table_.appendChild(tbody_);
      document.getElementsByTagName('body')[0].appendChild(table_);

      true_or_false("42"       );
      true_or_false("0"        );
      true_or_false('"0"'      );
      true_or_false('""'       );
      true_or_false("Infinity" );
      true_or_false("undefined");
      true_or_false("NaN"      );
      true_or_false("20 > 10"  );

    }

  </script>

</head>
<body onload='main()';>
</body>
</html>
Github repository about-javascript, path: /types/Boolean/true_vs_false.html

See also

Code snippets related to the global object
String related code snippets
Snippets with the RegExp object
Unicode related code snippets.
JavaScript

Index