<button id="btn1">Click Me</button> <input type="button" id="btn1" value="Click Me">
<!DOCTYPE html>
<html>
<body>
<script>
document.writeln("Hello, World.");
</script>
</body>document.writeln("Hello, World.");
withconsole.log("Hello, World.");
Refresh the browser.
Writing to the browser console is useful for debugging.
alert("Hello, World.");
instead of
console.log("Hello, World.");
// BasicFunction Example
// Function definition:
function inToCm(inches) {
cm = inches * 2.54;
return cm;
}
// Function test:
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
document.writeln("abc");
document.writeln("def");
Why are the outputs on the same line?
I thought that writeln puts a newline
character (\n) at the end of its output. Place these statements
in this test script before viewing the HTML page:
<html>
<body>
<script>
// Copy JavaScript code to test here.
</script>
</body>
</html>
Answer: Just because the HTML code goes to a new line doesn't mean
that the browser will display it on a new line. The \n character counts
as whitespace that is eliminated by the browser.
You need <br> to force
the browser display to go to a new line:
document.writeln("abc<br>");
document.writeln("def");
foldera
/ | \
folderb s.css page1.html
/
page2.html
<!DOCTYPE html>
<html>
<body>
<script>
// Copy JavaScript code to test here.
</script>
</body>
</html>
<!DOCTYPE html>
<!-- MaxValue Example -->
<html>
<body>
<script>
var n = 2;
// Repeat for loop 11 times.
for(var i = 1; i <= 11; i++) {
document.write(n + " ");
n = n * n;
}
document.writeln("<br><br>");
document.writeln(
"<br>Maximum Number Value:<br>" +
Number.MAX_VALUE + "<br>");
</script>
</body>
</html>
// Output:
2 4 16 256 65536 4294967296 18446744073709552000
3.402823669209385e+38 1.157920892373162e+77
1.3407807929942597e+154 Infinity
Maximum Number Value: 1.7976931348623157e+308
document.writeln(1.0e500 + " "); document.writeln(0 / 0 + " "); document.writeln(5 / "W" + " "); // Output: Infinity NaN NaN
// Undefined Example var s; document.writeln(s + "<br>"); var t = null; document.writeln(t + "<br>"); // Output: undefined null
document.write(Boolean(5) + " ");
document.write(Boolean(283764) + " ");
document.write(Boolean(0) + " ");
document.write(Boolean(-203) + "<br>");
document.write(Boolean(3.14159265) + " ");
document.write(Boolean(-284.378) + " ");
document.write(Boolean(0.000000001) + " ");
document.write(Boolean(0.0) + "<br>");
document.write(Boolean("grapefruit") + " ");
document.write(Boolean("$%&*@") + " ");
document.write(Boolean("0") + " ");
document.write(Boolean("false") + " ");
document.write(Boolean(" ") + " ");
document.write(Boolean("") + "<br>");
document.write(Boolean(null) + " ");
document.write(Boolean(undefined) + " ");
document.write(Boolean(Infinity) + " ");
document.write(Boolean(NaN) + "<br>");
Then run it to check if your prediction is correct.
var n = 45;
var p;
if (n) {
v = "nonzero";
}
else {
v = "zero";
}
document.writeln("Value: " + v);
function makeGreeting(name) {
var greeting = `Hello, ${name}, how are you?`;
return greeting
}
document.writeln(greeting);
The variable greeting does not exist after the end of
the function.
{
let x = 1.5;
x++;
}
document.writeln(x);
These statements generate an error because the variable x does not exist after the block in
which it is defined ends.
// Compute the sum of numbers between
// 1 and n inclusive:
function sum1ToN(n) {
var sum = 0;
for(let i = 1; i <= n; i++) {
sum += i;
}
return sum;
}
document.writeln("Sum: " + sum1ToN(100));
// Output:
5050
// Note: a more efficient way to compute
// the sum of the numbers from 1 to 100
// is to use this formula:
// sum = (n * (n + 1)) / 2
= (100 * 101) / 2
= 10100 / 2
= 5050
This last forrmula is part of the famous mathematician Karl Gauss's story. When he was in elementary schools
(some versions say Kindergarten),
the teacher asked the class asked the students to add up the numbers from 1 to 100. Although this would
be a difficult problem for most elementary students, Karl Gauss noticed that
sum = 1 + 2 + 3 + ... + 98 + 99 + 100
= (1 + 100) + (2 + 99) + (3 + 98) + ... + (50 + 51)
= 101 * 50
= 5050
and announced the answer of 5,050 in less than one minute.
let x = alert("Enter a positive integer.");
if (x % 2 == 0) {
var parity = "even";
}
else {
var parity = "odd";
}
document.writeln(`Parity: ${parity}`);