var a = 45.3; document.writeln(a + " " + typeof a + "<br>"); // Output: 45.3 number var b = "pomegranate"; document.writeln(b + " " + typeof b + "<br>"); // Output: pomegranate string var c = false; document.writeln(c + " " + typeof c + "<br>"); // Output: false boolean var d = 1384 / 0; document.writeln(d + " " + typeof d + "<br>"); // Output: Infinity number var e = "w" / 25; document.writeln(e + " " + typeof e + "<br>"); // Output: NaN number var f; document.writeln(f + " " + typeof f + "<br>"); // Output: undefined undefined var x = 1.43e678; document.writeln(x + " " + typeof x + "<br>"); // Output: Infinity number var g = "123", h = 123; document.writeln((g == h) + " " + (g === h) + "<br>"); // Output: true false document.writeln("abc"); document.writelm("def"); document.writeln("ghi"); // Output: abc // Note that document.writelm is not a method, so the // JavaScript script stops here and displays an error // message in the debugging console.
bigint boolean number string symbol undefined null
function s(x) { return x ** 2; } document.writeln(typeof(s)); // Output: function
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> <!-- Method 1. Use onclick attribute in button. All source code is in one file. --> <html lang="en"> <head> <meta charset="UTF-8"> <title>Test Button</title> <script> function changeValue( ) { var button1 = document.getElementById("btn1"); button1.value = "I've been clicked."; } </script> </head> <body> <h1>Test Button</h1> <input id = "btn1" type="button" value="Click Me" onclick="changeValue( );"> </body> </html> ====================================================== <!DOCTYPE html> <!-- Method 2. Use addEventListener method to attach event listener to button. Put HTML, CSS, and JS code in separate files. Source code file: index.html --> <html lang="en"> <head> <meta charset="UTF-8"> <title>Test Button</title> <link rel="stylesheet" href="styles.css"> <script src="script.js"></script> </head> <body> <h1>Test Button</h1> <input id = "btn1" type="button" value="Click Me"> </body> </html> ---------------------------------------------------------- /* Source code file: styles.css */ body { font-family: Verdana; background-color: #F0F0FF; color: #000040; } ---------------------------------------------------------- // Source code file: script.js function changeValue( ) { var button1 = document.getElementById("btn1"); button1.value = "I've been clicked."; } function init( ) { var button1 = document.getElementById("btn1"); button1.addEventListener("click", changeValue); } 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.
var a = [2, 3, 5, 7, 11, 13]; document.writeln(a + "<br>"); document.writeln(a[4]);
a0 = 2; a1 = 3; a2 = 5; a3 = 7; a4 = 11; a5 = 13;
for(let i = 0; i < 5; i++) { document.write(a[i] + " "); } // Output: 2 3 5 7 11 13or
for(let n of a)) { document.write(n + " "); } // Output: 2 3 5 7 11 13
var a = [45.3, 'apple', false, null];
var a = [2, 3, 5, 7]; document.writeln(typeof a); // Output: object // Reason: an array is not a primitive datatype, it // is a composite datatype called an object type.