- Write reusable JavaScript functions
- Distinguish parameters and arguments
- Understand function return values
- Handle browser events with listeners
- Use the event object in interactions
BMC201 - Web Technology
2026-02-16
Lecture 13
JavaScript Functions & Event Handling
Week 4 | Unit II: JavaScript Functions & Events
BMC201 - Web Technology
Mr. Prashant Kumar Nag, Assistant Professor
Learning Objectives
Why Functions Matter
Functions help avoid repeated code
Functions make code easier to test and debug
Functions improve readability and maintenance
Function Declaration vs Expression
function add(a, b) {
return a + b;
}
const multiply = function(a, b) {
return a * b;
};
const square = x => x * x;add is declared; multiply is stored in a variable; square uses arrow syntax.
Parameters, Arguments, Return
function calculateTotal(price, taxRate) {
const tax = price * taxRate;
const total = price + tax;
return total;
}
const bill = calculateTotal(1000, 0.18);price, taxRate1000, 0.18What Are Events?
An event is an action in the browser:
Common DOM Events
element.addEventListener("click", handleClick);
element.addEventListener("change", handleChange);
element.addEventListener("input", handleInput);
form.addEventListener("submit", handleSubmit);
window.addEventListener("load", initPage);Pick event type based on user interaction you want to capture.
Using the Event Object
form.addEventListener("submit", function(event) {
event.preventDefault();
console.log(event.type);
console.log(event.target);
console.log("Form submission intercepted");
});preventDefault() stops default behaviortarget gives the source elementInteractive Counter Demo
Live Classroom Demo: Functions & Events
Use this interactive page during class to demonstrate:
click, input, and submit eventsevent.preventDefault() in form handlingAlternative site path: /html/demos/lecture-13-demo.html
Best Practices
validateEmail()addEventListener over old event attributesQuestions?
Next: Lecture 14 - Conditions & Looping