0% found this document useful (0 votes)
3 views

Assignment

The document discusses events and event handlers in JavaScript, explaining that events are user interactions or system operations that trigger responses in the browser. It illustrates how to create an event listener for a button click, which executes a function when triggered. Additionally, it covers JavaScript objects as composite data types that store related data and functionality through key-value pairs, enabling modular and reusable code.

Uploaded by

senic printers
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Assignment

The document discusses events and event handlers in JavaScript, explaining that events are user interactions or system operations that trigger responses in the browser. It illustrates how to create an event listener for a button click, which executes a function when triggered. Additionally, it covers JavaScript objects as composite data types that store related data and functionality through key-value pairs, enabling modular and reusable code.

Uploaded by

senic printers
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Web1 ASSIGNMENT

Name: Mathew B.Wambugu Mwangi.

Reg No: IN13/00096/22.

Lecturer: Jasper Abongo.

QUESTION 1: Write on Event and Event handler.

A: EVENTS.

Events in JavaScript are actions or occurrences that happen in the browser, triggered by user

interaction or system operations. Common events include clicks, keypresses, mouse

movements, and page loading. JavaScript code can respond to these events by executing

predefined functions, enabling interactive web experiences.

<html>

<head>

<title>Event Example</title>

</head>

<body>

<button id="myButton">Click me!</button>


<script>

// Add event listener to the button

document.getElementById("myButton").addEventListener("click", function() {

alert("Button clicked!");

});

</script>

</body>

</html>

B: EVENT HANDLER.

An event handler in JavaScript is a function attached to an event listener, triggered by user

actions or system events. It executes code in response, enabling dynamic and interactive

behavior in web applications.

2: OBJECTS.

Objects in JavaScript are composite data types used to store related data and functionality. They

consist of key-value pairs where keys are strings (properties) and values can be any data type or

function (methods). Objects enable encapsulation, organization, and manipulation of data,

facilitating modular and reusable code.

Example 1: Object literal.


const animal = {

species: "Dog",

age: 5,

sound: "Woof",

speak: function() {

console.log(this.sound);

In these examples, the first one defines An animal object using an object literal, containing

properties like name and age, along with the sound.

You might also like