JavaScript
JavaScript
JavaScript
3. JavaScript Comments
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript Single-line Comment</title>
</head>
<body>
<script>
// This is my first JavaScript program
document.write("Hello World!");
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript Multi-line Comment</title>
</head>
<body>
<script>
/* This is my first program
in JavaScript */
document.write("Hello World!");
</script>
</body>
</html>
JavaScript Variables
There are three ways to declare variables in JavaScript:
1. var,
2. let
3. const.
• A variable name must start with a letter, underscore (_), or dollar sign ($).
• A variable name cannot start with a number.
• A variable name can only contain alpha-numeric characters (A-z, 0-9) and
underscores.
• A variable name cannot contain spaces.
• A variable name cannot be a JavaScript keyword or a JavaScript reserved word.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Creating Variables in JavaScript</title>
</head>
<body>
<script>
// Creating variables
let name = "Peter Parker";
let age = 21;
let isMarried = false;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Declaring Variables in JavaScript</title>
</head>
<body>
<script>
// Declaring Variable
let userName;
// Assigning value
userName = "Clark Kent";
// Trying to reassign
PI = 10; // error
</script>
<p><strong>Note:</strong> Please check out the browser console by pressing the f12 key on the
keyboard.</p>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript NaN</title>
</head>
<body>
<script>
document.write("Some text" / 2);
document.write("<br>");
document.write("Some text" / 2 + 10);
document.write("<br>");
document.write(Math.sqrt(-1));
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript Infinity</title>
</head>
<body>
<script>
document.write(16 / 0);
document.write("<br>");
document.write(-16 / 0);
document.write("<br>");
document.write(16 / -0);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript Comparisons</title>
</head>
<body>
<script>
let a = 2, b = 5, c = 10;
b = null;
document.write(b) // Print: null
</script>
</body>
</html>
// Strings
document.write(typeof '' + "<br>"); // Prints: "string"
document.write(typeof 'hello' + "<br>"); // Prints: "string"
document.write(typeof '12' + "<br>"); // Prints: "string". Number within quotes is
document.write(typeof string
// Booleans
document.write(typeof true + "<br>"); // Prints: "boolean"
document.write(typeof false + "<br>"); // Prints: "boolean"
// Undefined
document.write(typeof undefined + "<br>"); // Prints: "undefined"
document.write(typeof undeclaredVariable + "<br>"); // Prints: "undefined"
// Null
document.write(typeof Null + "<br>"); // Prints: "object"
// Objects
document.write(typeof {name: "John", age: 18} + "<br>"); // Prints: "object"
// Arrays
document.write(typeof [1, 2, 4] + "<br>"); // Prints: "object"
// Functions
document.write(typeof function(){}); // Prints: "function"
</script>
</body>
</html>