🌟 1.
Hello World Program
<script>
document.write("<i>Hello World!</i>");
</script>
📥 2. Input and Output Examples
➤ Using document.write()
<script>
document.write("Beware of Cyber Scams");
</script>
➤ Using alert() Box
<script>
alert("You have chosen an Alert box");
</script>
➤ Using prompt() and document.write()
<script>
var name = prompt("Enter your name:");
var age = prompt("Enter your age:");
document.write("Your name is: " + name + "<br>");
document.write("Your age is: " + age);
</script>
➤ Using confirm()
<script>
var result = confirm("Are you sure you want to cancel the order?");
if(result == true) {
document.write("Order IS Canceled!");
} else {
document.write("Order NOT Canceled");
}
</script>
3. DOM Interaction using getElementById()
➤ Output into a paragraph:
<p id="demo"></p>
<script>
document.getElementById("demo").inner = "Destination India!";
</script>
➤ Button Click Example:
<p>Click the button</p>
<button onclick="myFunction()">Click me</button>
<p id="demo"></p>
<script>
function myFunction() {
document.getElementById("demo").inner = "Hello World";
}
</script>
4. Arithmetic Operations
<script>
var a = 10, b = 5, c;
c = a + b;
document.write("a + b = " + c + "<br>");
c = a - b;
document.write("a - b = " + c + "<br>");
c = a * b;
document.write("a * b = " + c + "<br>");
c = a / b;
document.write("a / b = " + c + "<br>");
c = a % b;
document.write("a % b = " + c + "<br>");
</script>
5. Comparison Operators
<script>
var a = 10, b = 5;
document.write("a == b: " + (a == b) + "<br>");
document.write("a != b: " + (a != b) + "<br>");
document.write("a > b: " + (a > b) + "<br>");
</script>
6. Assignment Operators
<script>
var a = 10;
a += 5;
document.write("a after a += 5: " + a + "<br>");
a *= 2;
document.write("a after a *= 2: " + a + "<br>");
</script>
🔁 7. Loops in JavaScript
➤ For Loop (Print 1 to 10)
<script>
for(var i = 1; i <= 10; i++) {
document.write(i + "<br>");
}
</script>
➤ While Loop (Print square of numbers 1–5)
<script>
var i = 1;
while(i <= 5) {
var square = i * i;
document.write("Square of " + i + ": " + square + "<br>");
i++;
}
</script>
✅ 8. If-Else Statement
<script>
var marks = prompt("Enter marks:");
if(marks >= 30) {
document.write("Pass");
} else {
document.write("Fail");
}
</script>
🔘 9. Switch-Case Statement
<script>
var grade = prompt("Enter grade (A/B/C):");
switch(grade) {
case 'A': document.write("Excellent"); break;
case 'B': document.write("Very Good"); break;
case 'C': document.write("Good"); break;
default: document.write("No Grade");
}
</script>
📌 10. Inbuilt Functions
➤ parseInt() and parseFloat()
<script>
var x = "34.56";
document.write(parseInt(x) + "<br>"); // Output: 34
document.write(parseFloat(x) + "<br>"); // Output: 34.56
</script>
➤ typeof and isNaN()
<script>
var a = "hello", b = 123;
document.write(typeof a + "<br>"); // string
document.write(typeof b + "<br>"); // number
document.write(isNaN("book") + "<br>"); // true
document.write(isNaN(45) + "<br>"); // false
</script>