Part 1 – JavaScript Basics (Tutorial Notes)
1. Introduction to JavaScript
JavaScript is a lightweight, interpreted programming language used to make web pages
interactive.
It allows you to add dynamic behavior such as animations, form validations, and responsive
actions.
It runs directly in the browser and works alongside HTML and CSS.
Example:
<script>
alert('Welcome to JavaScript!');
</script>
Output: A popup box saying “Welcome to JavaScript!” appears in the browser.
2. Setting Up the Environment
You can run JavaScript in two main ways:
1. Directly in the browser console (Right-click → Inspect → Console tab).
2. In an external file linked with HTML using the <script> tag.
Example HTML with linked JS file:
<!DOCTYPE html>
<html>
<head><title>JS Example</title></head>
<body>
<script src="[Link]"></script>
</body>
</html>
3. Syntax Rules
- Every statement ends with a semicolon (;).
- JavaScript is case-sensitive.
- Use // for single-line comments and /* */ for multi-line comments.
Example:
// This is a comment
let x = 10; // Variable declaration
[Link](x);
Output: 10
4. Variables and Constants
Variables store data values. You can declare them using `var`, `let`, or `const`.
- `var`: function-scoped, can be redeclared.
- `let`: block-scoped, cannot be redeclared in same scope.
- `const`: block-scoped, cannot be changed once assigned.
Example:
let name = "Suryakant";
const age = 21;
[Link](name, age);
Output: Suryakant 21
5. Data Types
JavaScript has 7 primitive types:
1. String
2. Number
3. Boolean
4. Undefined
5. Null
6. Symbol
7. BigInt
Example:
let x = 10;
let y = "Hello";
let z = true;
[Link](typeof x, typeof y, typeof z);
Output: number string boolean
6. Operators
Operators perform operations on data.
- Arithmetic: +, -, *, /, %
- Assignment: =, +=, -=
- Comparison: ==, ===, !=, >, <
- Logical: &&, ||, !
- Ternary: condition ? value1 : value2
Example:
let a = 5, b = 3;
[Link](a + b); // 8
[Link](a > b ? "A is greater" : "B is greater");
Output:
8
A is greater
7. Conditional Statements
Conditional statements control program flow based on conditions.
Example:
let marks = 85;
if (marks >= 90) {
[Link]("Grade A");
} else if (marks >= 75) {
[Link]("Grade B");
} else {
[Link]("Grade C");
}
Output: Grade B
8. Loops
Loops execute a block of code multiple times.
Types: for, while, do...while
Example – Sum of first 5 numbers:
let sum = 0;
for (let i = 1; i <= 5; i++) {
sum += i;
}
[Link](sum);
Output: 15
9. Example Programs
Example 1: Check even or odd number
let num = 7;
if (num % 2 === 0)
[Link]("Even");
else
[Link]("Odd");
Output: Odd
Example 2: Multiplication table of 5
for (let i = 1; i <= 10; i++) {
[Link](`5 x ${i} = ${5 * i}`);
}
Output:
5x1=5
5 x 2 = 10
... 5 x 10 = 50
10. Practice Exercises
1. Write a program to print numbers from 1 to 100.
2. Write a program to find the largest of three numbers.
3. Write a program to calculate factorial of a number.
4. Write a program to print Fibonacci series up to n terms.