[Go to site: main page, start]

0% found this document useful (0 votes)
4 views5 pages

JavaScript Beginner Examples

The document provides beginner-level examples of JavaScript, covering topics such as variables, arithmetic operators, conditional statements (if-else), and logical operators. Each example demonstrates basic programming concepts through HTML and JavaScript code snippets. The examples include string concatenation, arithmetic calculations, and grading logic based on scores.

Uploaded by

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

JavaScript Beginner Examples

The document provides beginner-level examples of JavaScript, covering topics such as variables, arithmetic operators, conditional statements (if-else), and logical operators. Each example demonstrates basic programming concepts through HTML and JavaScript code snippets. The examples include string concatenation, arithmetic calculations, and grading logic based on scores.

Uploaded by

p10604302
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JavaScript Basics – Beginner Level Examples

Example 1: Variables & String Concatenation

<!DOCTYPE html>
<html>
<body>
<script>
var name = "Riya";
var grade = 6;

[Link]("Student Name: " + name + "<br>");


[Link]("Grade: " + grade);
</script>
</body>
</html>

Example 2: Arithmetic Operators

<!DOCTYPE html>
<html>
<body>
<script>
var a = 20;
var b = 10;

[Link]("Addition: " + (a + b) + "<br>");


[Link]("Subtraction: " + (a - b) + "<br>");
[Link]("Multiplication: " + (a * b) + "<br>");
[Link]("Division: " + (a / b));
</script>
</body>
</html>

Example 3: If–Else Statement

<!DOCTYPE html>
<html>
<body>
<script>
var marks = 55;
if (marks >= 40) {
[Link]("Result: Pass");
} else {
[Link]("Result: Fail");
}
</script>
</body>
</html>

Example 4: If–Else If–Else (Grades)

<!DOCTYPE html>
<html>
<body>
<script>
var score = 78;

if (score >= 90) {


[Link]("Grade A+");
}
else if (score >= 75) {
[Link]("Grade A");
}
else if (score >= 60) {
[Link]("Grade B");
}
else {
[Link]("Grade C");
}
</script>
</body>
</html>

Example 5: Logical Operators & HTML Styling

<!DOCTYPE html>
<html>
<body>
<script>
var maths = 65;
var science = 70;
if (maths >= 40 && science >= 40) {
[Link]("<h2 style='color:green;'>PASS</h2>");
} else {
[Link]("<h2 style='color:red;'>FAIL</h2>");
}
</script>
</body>
</html>

Example 6:

<!DOCTYPE html>

<html>

<head>

<title>JavaScript Basics</title>

</head>

<body>

<script>

/* ===== 1. VARIABLES ===== */

var name = "Ayaan";

var age = 12;

var maths = 80;

var science = 70;

/* ===== 2. ARITHMETIC OPERATORS ===== */

var total = maths + science;

var average = total / 2;

/* ===== 3. STRING CONCATENATION ===== */

[Link]("<h2>Student Details</h2>");

[Link]("Name: " + name + "<br>");

[Link]("Age: " + age + "<br><br>");


/* ===== 4. DISPLAY CALCULATIONS ===== */

[Link]("<b>Total Marks:</b> " + total + "<br>");

[Link]("<b>Average Marks:</b> " + average + "<br><br>");

/* ===== 5. LOGICAL OPERATORS ===== */

// AND (&&) operator

if (maths >= 40 && science >= 40) {

[Link]("<p style='color:green;'>Result: PASS</p>");

} else {

[Link]("<p style='color:red;'>Result: FAIL</p>");

/* ===== 6. IF – ELSE IF – ELSE ===== */

[Link]("<h3>Grade</h3>");

if (average >= 90) {

[Link]("Grade: A+");

else if (average >= 75) {

[Link]("Grade: A");

else if (average >= 60) {

[Link]("Grade: B");

else {

[Link]("Grade: C");
}

</script>

</body>

</html>

You might also like