var a = 45.3;
document.writeln(a + " " + typeof a + "<br>");
// Output: 45.3 number
var b = -163;
document.writeln(b + " " + typeof b + "<br>");
// Output: -163 number
var c = "pomegranate";
document.writeln(c + " " + typeof c + "<br>");
// Output: pomegranate string
var d = false;
document.writeln(d + " " + typeof d + "<br>");
// Output: false boolean
var e = 1384 / 0;
document.writeln(e + " " + typeof e + "<br>");
// Output: Infinity number
var f = "w" / 25;
document.writeln(f + " " + typeof f + "<br>");
// Output: NaN number
var f2 = 0 / 0;
document.writeln(f2 + " " + typeof f2 + "<br>");
// Output: NaN number
var g;
document.writeln(g + " " + typeof g + "<br>");
// Output: undefined undefined
var h = 1.43e678;
document.writeln(h + " " + typeof h + "<br>");
// Output: Infinity number
document.writeln("abc");
document.writelm("def");
document.writeln("ghi");
// Output: abc
// In Line 2, writeln is mispelled as writelm.
// Open the Chrome Console to see the error message.
var i = 1, n = 2;
while(i <= 11) {
document.write(n + " ");
i++;
n = n * n;
}
document.writeln("<br><br>");
document.writeln(Number.MAX_VALUE);
// Output:
2 4 16 256 65536 4294967296 18446744073709552000 3.402823669209385e+38
1.157920892373162e+77 1.3407807929942597e+154 Infinity
1.7976931348623157e+308
bigint boolean number string symbol undefined null
function s(x) {
return x ** 2;
}
document.writeln(typeof(s));
// Output:
function
// BasicFunction Example
// Function definition:
function inToCm(inches) {
cm = inches * 2.54;
return cm;
}
// Test the function:
var inches = prompt("Enter length in inches", "0");
var cm = inToCm(inches);
document.writeln("Length in cm: " + cm);
// If input is 2, the output is 5.08
| Method Call | Return Value |
|---|---|
| Math.floor(2.38) | 2.0 |
| Math.floor(0.998) | 0.0 |
| Math.floor(-6.83) | -7.0 |
| Method Call | Return Value |
|---|---|
| Math.ceil(2.38) | 3.0 |
| Math.ceil(0.001) | 1.0 |
| Math.floor(-6.83) | -6.0 |
==========================================
------------------------------------------
<!DOCTYPE html>
<!-- ButtonTest Example
Source code file: index.html-->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ButtonTest Example</title>
<link rel="stylesheet" href="styles.css">
<script src="script.js"></script>
</head>
<body>
<h1>Button Test Example</h1>
<button id="btn1">Click Me</button>
</body>
</html>
------------------------------------------
/* ButtonTest Example
Source code file: styles.css */
body { font-family: Verdana, sans-serif; }
button { width: 150px; }
------------------------------------------
/ ButtonTest Example
// Source code file: script.js
// Event listener
function changeCaption( ) {
var button1 = document.getElementById("btn1");
button1.innerHTML = "I\'ve been clicked.";
}
// init method adds event listener to button.
function init( ) {
var button1 = document.getElementById("btn1");
button1.addEventListener("click", changeCaption);
}
// init method executes after page is loaded.
window.addEventListener("load", init);
==========================================
| m | = |
|
// Test the computeMP function, which computes the monthly
// payment for a loan. The function parameters are
// p=principal, r=interest rate, n=term of loan in years.
function computeMP(p, r, n) {
var mp = (p * r / 1200.0) /
(1 - (1.0 + r / 1200.0) ** (-12.0 * n));
return mp;
}
var principal = 100000;
var rate = 6;
var term = 15;
var mp = computeMP(principal, rate, term);
mp = Math.round(mp * 100) / 100;
document.writeln(mp);
// Output should be 843.86.