Unit 2: Scripting
Introduction to JavaScript
JavaScript (JS) is a client-side scripting language used to make web pages interactive and dynamic. It works with HTML and CSS to improve user experience.
Features of JavaScript
- Lightweight and fast
- Runs inside the browser
- Object-based language
- Supports event-driven programming
- Platform independent
Uses of JavaScript
| Use | Example |
|---|---|
| Form validation | Checking empty fields |
| Dynamic content | Changing text/images |
| User interaction | Buttons, alerts |
| Calculations | EMI, totals |
Creating Variables in JavaScript
Variables are used to store data values.
Variable Keywords
| Keyword | Description |
|---|---|
var | Old method, function-scoped |
let | Block-scoped, recommended |
const | Constant value, cannot change |
Example
Rules for Variable Names
- Must start with a letter,
_, or$ - Cannot use keywords
- Case-sensitive
Creating Functions in JavaScript
A function is a block of code that performs a specific task and can be reused.
Function Syntax
Example
Function with Parameters
UI Events in JavaScript
UI Events occur when users interact with the webpage.
Common UI Events
| Event | Description |
|---|---|
onclick | Mouse click |
onmouseover | Mouse over element |
onmouseout | Mouse leaves |
onchange | Input value changes |
onkeyup | Key released |
onsubmit | Form submission |
Example
Returning Data from Functions
Functions can return values using the return keyword.
Example
Key Points
returnsends data back to the caller- Code after
returndoes not execute - One function can return only one value
Working with Conditions
Conditional statements allow decision-making in programs.
Types of Conditional Statements
1. if Statement
2. if-else Statement
3. else-if Ladder
4. switch Statement
Comparison Table
| Concept | Purpose |
|---|---|
| Variables | Store data |
| Functions | Reusable logic |
| Events | User interaction |
| Conditions | Decision making |
Summary
- JavaScript adds interactivity to web pages
- Variables store values using
var,let,const - Functions make code reusable
- Events respond to user actions
- Conditions control program flow
Looping in JavaScript
Loops are used to execute a block of code repeatedly until a condition is satisfied.
Types of Loops in JavaScript
1. for Loop
Used when the number of iterations is known.
2. while Loop
Executes as long as the condition is true.
3. do-while Loop
Executes at least once, even if the condition is false.
4. for...of Loop
Used to iterate over arrays.
Loop Control Statements
| Statement | Purpose |
|---|---|
break | Stops loop execution |
continue | Skips current iteration |
Block Scope Variables
Block scope means variables are accessible only within the block { } in which they are declared.
Block-Scoped Keywords
| Keyword | Scope |
|---|---|
let | Block scope |
const | Block scope |
var | Function scope |
Example
Advantages of Block Scope
- Prevents variable conflicts
- Improves code security
- Easier debugging
Working with Objects
In JavaScript, an object is a collection of key-value pairs.
Example Object
Accessing Object Properties
Creating Object using Object Literals
Object literals are the simplest way to create objects.
Syntax
Example with Method
Advantages of Object Literals
- Easy to create
- Less code
- Readable structure
Manipulating DOM Elements with JavaScript
DOM (Document Object Model) represents the structure of an HTML document as objects.
Common DOM Methods
| Method | Use |
|---|---|
getElementById() | Select element by ID |
getElementsByClassName() | Select by class |
getElementsByTagName() | Select by tag |
querySelector() | Select first match |
querySelectorAll() | Select all matches |
Changing Content
Changing Style
Handling Events with DOM
DOM Manipulation Tasks
| Task | Method |
|---|---|
| Change text | innerHTML |
| Change style | style.property |
| Add element | createElement() |
| Remove element | remove() |
Summary Table
| Topic | Key Concept |
|---|---|
| Loops | Repetition |
| Block Scope | Secure variables |
| Objects | Key-value data |
| Object Literals | Easy object creation |
| DOM | HTML manipulation |