JavaScript Codes (1)
JavaScript Codes (1)
<!doctype html>
<html>
<head>
<title>hello world></title>
</head>
<body>
<script>document.write("hello world");
</script>
</body>
</html>
!DOCTYPE html>
<html>
<head>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
// String
// Number
// Boolean
// Undefined
let city;
// Null
// Array
// Object
let person = {
firstName: "John",
lastName: "Doe",
age: 30
};
document.write("Person: " + person.firstName + " " + person.lastName + ", Age: " + person.age);
</script>
</body>
</html>
3. Using operators
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Operators</title>
</head>
<body>
<script>
// Arithmetic Operators
let a = 10;
let b = 5;
document.write("<b>Arithmetic Operators:</b><br>");
let x = 15;
document.write("<b>Assignment Operators:</b><br>");
x += 5;
x *= 2;
// Comparison Operators
document.write("<b>Comparison Operators:</b><br>");
// Logical Operators
let p = true;
let q = false;
document.write("<b>Logical Operators:</b><br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
// Example 1: if statement
if (number > 0) {
document.write("Number is positive.<br>");
} else {
}
// Example 3: if-else if statement
document.write("Grade: A+<br>");
document.write("Grade: A<br>");
document.write("Grade: B<br>");
document.write("Grade: C<br>");
} else {
document.write("Result: Failed<br>");
</script>
</body>
</html>
5. Loop statements
<!DOCTYPE html>
<html>
<head>
</head>
<body>
// For loop
document.write("<b>For Loop:</b><br>");
// While loop
document.write("<br><b>While Loop:</b><br>");
let count = 1;
count++;
// Do...while loop
document.write("<br><b>Do...While Loop:</b><br>");
let j = 1;
do {
j++;
</script>
</body>
</html>
6. Using function
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Functions</title>
</head>
<body>
<script>
function greetUser(name) {
function addNumbers(a, b) {
let sum = a + b;
document.write("Sum of " + a + " and " + b + " is: " + sum + "<br>");
function square(number) {
greetUser("Alice");
greetUser("Bob");
addNumbers(5, 10);
addNumbers(12, 8);
</script>
</body>
</html>