🔑 JavaScript Essentials (Detailed Notes)
1. Introduction
JavaScript is a scripting language mainly used for web development.
Runs in the browser to make websites interactive (e.g., buttons, forms, animations).
It is case-sensitive.
2. Variables
Used to store data.
Declared using:
o var → old way, function-scoped.
o let → modern, block-scoped.
o const → constant, cannot be reassigned.
Example:
js
let name = "Khadija";
const pi = 3.14;
3. Data Types
Primitive types: string, number, boolean, null, undefined.
Objects: arrays, functions, custom objects.
Example:
js
let age = 17; // number
let student = true; // boolean
let fruits = ["Apple", "Banana"]; // array
4. Operators
Arithmetic: + - * / %
Comparison: == === != !== > < >= <=
Logical: && || !
Assignment: = += -= *= /=
5. Conditional Statements
Used for decision-making.
js
if (marks >= 90) {
[Link]("Grade A");
} else if (marks >= 70) {
[Link]("Grade B");
} else {
[Link]("Fail");
}
6. Loops
for loop → repeat fixed times.
while loop → repeat until condition false.
do...while → runs at least once.
js
for (let i = 1; i <= 5; i++) {
[Link](i);
}
7. Functions
Block of code that performs a task.
js
function greet(name) {
return "Hello " + name;
}
[Link](greet("Khadija"));
8. Events
JavaScript reacts to user actions (click, hover, input).
html
<button onclick="alert('Hello!')">Click Me</button>
9. Arrays
Store multiple values in one variable.
js
let colors = ["Red", "Green", "Blue"];
[Link](colors[0]); // Red
10. Objects
Store data in key-value pairs.
js
let student = {
name: "Khadija",
age: 17,
grade: "A"
};
[Link]([Link]);
11. DOM Manipulation
JavaScript can change HTML content.
js
[Link]("demo").innerHTML = "Hello World!";
12. Input & Output
alert("Message") → popup box.
[Link]("Message") → output in console.
prompt("Enter name") → input from user.
13. Errors & Debugging
Common mistakes: missing semicolon, wrong variable name, mismatched quotes.
Use [Link]() to debug.
14. Important Exam Examples
Hello World alert box:
js
alert("Hello, World!");
Calculate Grade function:
js
function CalculateGrade(marks) {
if (marks >= 90) return "A";
else if (marks >= 70) return "B";
else if (marks >= 50) return "C";
else return "F";
}
JavaScript addition vs subtraction:
js
let a = 10;
let b = "5";
[Link](a + b); // "105" (string concatenation)
[Link](a - b); // 5 (number subtraction)
🎯 Exam-Focused Checklist
✅ Keywords: var, let, const
✅ Alert box syntax
✅ Difference between == and ===
✅ Arrays & Objects basics
✅ Event handling (onclick)
✅ Simple function for grading
✅ JavaScript data type quirks (string + number)