Javascript Boolean and Array – oh my!

Tags:

<html>
<script type=text/javascript>
document.write("Never use new Boolean.<br>");
var b = new Boolean("false");
if (b) {
  document.write("new Boolean is an Object and it's always true.<br>");
}

var oh_my_god = new Boolean(false)
if (oh_my_god) {
  document.write("I told you. Object is true!<br>");
}

document.write("Also, stay away from new Array. Use [].<br>");
function PrintArray(ar) {
  for (var i = 0; i < ar.length; ++i) {
    document.write(ar[i]);
  }
  document.write("<br>");
}

document.write("See. new Array is confusing.<br>");
PrintArray(new Array(10));
PrintArray(new Array(10, 20));

document.write("[elem1, elem2, ...] is obvious.<br>");
PrintArray([10]);
PrintArray([10, 20]);

</script>
</html>

Output:
Never use new Boolean.
new Boolean is an Object and it’s always true.
I told you. Object is true!
Also, stay away from new Array. Use [].
See. new Array is confusing.
undefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefined
1020
[elem1, elem2, …] is obvious.
10
1020