1. JavaScript Basics & Syntax

1. Print “Hello, World!” on Webpage

Program 1: document.write()

				
					<!DOCTYPE html>
<html>
<body>
<script>
document.write("Hello, World!");
</script>
</body>
</html>

				
			

Program 2: innerHTML

				
					<!DOCTYPE html>
<html>
<body>
<h2 id="output"></h2>
<script>
document.getElementById("output").innerHTML = "Hello, World!";
</script>
</body>
</html>

				
			

Program 3: Creating Element Dynamically

				
					<!DOCTYPE html>
<html>
<body>
<script>
const p = document.createElement("p");
p.textContent = "Hello, World!";
document.body.appendChild(p);
</script>
</body>
</html>

				
			

2. Print “Hello, World!” in Console

Program 1: Simple Console Log

				
					<!DOCTYPE html>
<html>
<body>
<script>
console.log("Hello, World!");
</script>
</body>
</html>

				
			

Program 2: Console with Variable

				
					<script>
let message = "Hello, World!";
console.log(message);
</script>

				
			

Program 3: Console with Label

				
					<script>
console.log("Message:", "Hello, World!");
</script>

				
			

3. Display Current Date

Program 1: Full Date Object

				
					<script>
let today = new Date();
document.write(today);
</script>

				
			

Program 2: Readable Date

				
					<script>
let today = new Date();
document.write(today.toDateString());
</script>

				
			

Program 3: Custom Date Format

				
					<script>
let d = new Date();
document.write(d.getDate() + "-" + (d.getMonth()+1) + "-" + d.getFullYear());
</script>

				
			

4. Display Current Time

Program 1: Local Time

				
					<script>
let now = new Date();
document.write(now.toLocaleTimeString());
</script>

				
			

Program 2: Hours, Minutes, Seconds

				
					<script>
let t = new Date();
document.write(t.getHours() + ":" + t.getMinutes() + ":" + t.getSeconds());
</script>

				
			

Program 3: Digital Time (12-hour)

				
					<script>
let t = new Date();
let hours = t.getHours() % 12 || 12;
document.write(hours + ":" + t.getMinutes());
</script>

				
			

5. Display Date and Time in Formatted Way

Program 1: Locale String

				
					<script>
let now = new Date();
document.write(now.toLocaleString());
</script>

				
			

Program 2: Custom Format (DD/MM/YYYY HH:MM)

				
					<script>
let d = new Date();
let formatted =
  d.getDate() + "/" +
  (d.getMonth()+1) + "/" +
  d.getFullYear() + " " +
  d.getHours() + ":" +
  d.getMinutes();
document.write(formatted);
</script>

				
			

Program 3: Using toDateString() + toTimeString()

				
					<script>
let d = new Date();
document.write(d.toDateString() + " " + d.toTimeString());
</script>

				
			

6. JavaScript Comments (Single-line & Multi-line)

Program 1: Single-line Comments

				
					<script>
// This is a single-line comment
document.write("JavaScript Comments");
</script>

				
			

Program 2: Multi-line Comments

				
					<script>
/*
This is a
multi-line comment
in JavaScript
*/
document.write("Multi-line Comment Example");
</script>

				
			

Program 3: Comments with Logic

				
					<script>
// Declaring a variable
let x = 10;

/* Printing the value
   on the webpage */
document.write(x);
</script>

				
			

7. Use of alert, prompt, and confirm

Program 1: alert()

				
					<script>
alert("Welcome to JavaScript!");
</script>

				
			

Program 2: prompt()

				
					<script>
let name = prompt("Enter your name:");
document.write("Hello " + name);
</script>

				
			

Program 3: confirm()

				
					<script>
let result = confirm("Do you want to continue?");
if(result){
  document.write("User clicked OK");
}else{
  document.write("User clicked Cancel");
}
</script>