0% found this document useful (0 votes)
0 views9 pages

JavaScript Codes (1)

The document provides a comprehensive overview of JavaScript programming, covering fundamental concepts such as printing output, data types, variables, operators, conditional statements, loops, and functions. Each section includes HTML examples demonstrating the usage of these concepts in JavaScript code. The document serves as a practical guide for beginners to understand and implement basic JavaScript functionalities.

Uploaded by

muhsinaasharaf9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views9 pages

JavaScript Codes (1)

The document provides a comprehensive overview of JavaScript programming, covering fundamental concepts such as printing output, data types, variables, operators, conditional statements, loops, and functions. Each section includes HTML examples demonstrating the usage of these concepts in JavaScript code. The document serves as a practical guide for beginners to understand and implement basic JavaScript functionalities.

Uploaded by

muhsinaasharaf9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

JavaScript

1. To print hello world

<!doctype html>

<html>

<head>

<title>hello world></title>

</head>

<body>

<script>document.write("hello world");

</script>

</body>

</html>

2. Using data types and variables

!DOCTYPE html>

<html>

<head>

<title>JavaScript Data Types</title>

<!DOCTYPE html>

<html>

<head>

<title>JavaScript Data Types</title>

</head>

<body>

<h2>JavaScript Data Types Output</h2>


<script>

// String

var name = "Alice";

document.write("Name: " + name + "<br>");

// Number

let age = 25;

document.write("Age: " + age + "<br>");

// Boolean

const isStudent = true;

document.write("Is student? " + isStudent + "<br>");

// Undefined

let city;

document.write("City: " + city + "<br>");

// Null

let score = null;

document.write("Score: " + score + "<br>");

// Array

let colors = ["red", "green", "blue"];

document.write("Colors: " + colors.join(", ") + "<br>");

// 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>

<h2>JavaScript Operators Example</h2>

<script>

// Arithmetic Operators

let a = 10;

let b = 5;

document.write("<b>Arithmetic Operators:</b><br>");

document.write("a + b = " + (a + b) + "<br>");

document.write("a - b = " + (a - b) + "<br>");

document.write("a * b = " + (a * b) + "<br>");

document.write("a / b = " + (a / b) + "<br>");

document.write("a % b = " + (a % b) + "<br><br>");


// Assignment Operators

let x = 15;

document.write("<b>Assignment Operators:</b><br>");

document.write("x = " + x + "<br>");

x += 5;

document.write("x += 5 → " + x + "<br>");

x *= 2;

document.write("x *= 2 → " + x + "<br><br>");

// Comparison Operators

let num1 = 10;

let num2 = "10";

document.write("<b>Comparison Operators:</b><br>");

document.write("num1 == num2 → " + (num1 == num2) + "<br>");

document.write("num1 === num2 → " + (num1 === num2) + "<br>");

document.write("num1 != num2 → " + (num1 != num2) + "<br>");

document.write("num1 !== num2 → " + (num1 !== num2) + "<br>");

document.write("num1 > 5 → " + (num1 > 5) + "<br><br>");

// Logical Operators

let p = true;

let q = false;

document.write("<b>Logical Operators:</b><br>");

document.write("p && q → " + (p && q) + "<br>");

document.write("p || q → " + (p || q) + "<br>");

document.write("!p → " + (!p) + "<br>");

</script>
</body>

</html>

4. Using conditional statements

<!DOCTYPE html>

<html>

<head>

<title>JavaScript Conditional Statements</title>

</head>

<body>

<h2>Conditional Statements Example</h2>

<script>

// Example 1: if statement

let number = 10;

if (number > 0) {

document.write("Number is positive.<br>");

// Example 2: if-else statement

let age = 17;

if (age >= 18) {

document.write("You are eligible to vote.<br>");

} else {

document.write("You are not eligible to vote.<br>");

}
// Example 3: if-else if statement

let marks = 85;

document.write("<br><b>Result based on Marks:</b><br>");

if (marks >= 90) {

document.write("Grade: A+<br>");

} else if (marks >= 75) {

document.write("Grade: A<br>");

} else if (marks >= 60) {

document.write("Grade: B<br>");

} else if (marks >= 40) {

document.write("Grade: C<br>");

} else {

document.write("Result: Failed<br>");

</script>

</body>

</html>

5. Loop statements

<!DOCTYPE html>

<html>

<head>

<title>JavaScript Loop Statements</title>

</head>

<body>

<h2>Loop Statements Example</h2>


<script>

// For loop

document.write("<b>For Loop:</b><br>");

for (let i = 1; i <= 5; i++) {

document.write("Number: " + i + "<br>");

// While loop

document.write("<br><b>While Loop:</b><br>");

let count = 1;

while (count <= 5) {

document.write("Count: " + count + "<br>");

count++;

// Do...while loop

document.write("<br><b>Do...While Loop:</b><br>");

let j = 1;

do {

document.write("Value: " + j + "<br>");

j++;

} while (j <= 5);

</script>

</body>

</html>

6. Using function

<!DOCTYPE html>
<html>

<head>

<title>JavaScript Functions</title>

</head>

<body>

<h2>Function Statements Example</h2>

<script>

// Function Declaration (Definition)

function greetUser(name) {

document.write("Hello, " + name + "!<br>");

// Function that adds two numbers

function addNumbers(a, b) {

let sum = a + b;

document.write("Sum of " + a + " and " + b + " is: " + sum + "<br>");

// Function that returns a value

function square(number) {

return number * number;

// Calling the functions

greetUser("Alice");

greetUser("Bob");
addNumbers(5, 10);

addNumbers(12, 8);

let result = square(6);

document.write("Square of 6 is: " + result + "<br>");

</script>

</body>

</html>

You might also like