To Become Familiar With JavaScript Basics for User Interaction
To Become Familiar With JavaScript Basics for User Interaction
JavaScript
It is a lightweight, case sensitive, client-side scripting language that allows interactions
with the user and creation of dynamic webpages. JavaScript (JS) is a programming
language mostly used to dynamically script webpages on the client side, but it is also
often utilized on the server-side, using packages such as Node.js. It enables you to create
dynamically updating content, control multimedia, animate images, and a lot of other
things. It is an open and cross platform language.
Advantages
• Less server interaction
• Immediate feedback to the visitors
• Increased interactivity
• Richer interfaces
Syntax
<script language="javascript" type="text/javascript">
<!--JavaScript code-->
</script>
JavaScript can be implemented using JavaScript statements that are placed within the
<script>... </script> HTML tags in a web page. Placing scripts at the bottom of the
<body> element improves the display speed, because script interpretation slows down
the display.
Simple JS program
HTML EVENTS
• An HTML event can be something the browser does, or
something a user does.
• JavaScript can be used to provide response when such events
occur.Here are some examples of HTML events:
o An HTML web page has finished loading
o An HTML input field was changed
o An HTML button was clicked etc
Functions
Function is a block of code that performs some task. The functions are called in
response to some events (button click, mouse hover etc…).
SYNTAX
JavaScript function can be created as follows:
function function_name ( argument list if any) {
// JavaScript code
}
Javascript Variables
Variables are named memory locations for storing values.
Variables in Javascript are declared with the “var” keyword.
JavaScript is untyped language.
JavaScript Regular Expressions:
Flags: There are several flags which are used to alter the behavior of a regular
expression. Flags may be appended to the end of a regex literal or they may be
specified as the second argument to the Regular expression.
Flag Description
Pattern Description
Quantifiers Description
Brackets Description
Lab Task
Task 1: Create a JavaScript program that can perform the basic arithmetic
operations.
Task 2: Create a simple To-Do List application using HTML forms, JavaScript
DOM manipulation, and event handlers.
This task creates a simple To-Do List application using HTML forms,
JavaScript DOM manipulation, and event handlers.
The HTML structure contains an input field for entering tasks, a button to
add tasks, and an unordered list (<ul>) to display the tasks.
The JavaScript file (todo.js) defines a function addTask() that adds a new
task to the list when the user clicks the "Add Task" button.
The function first gets the task text from the input field, creates a new list
item (<li>) with the task text, appends it to the task list, and clears the input
field.
If the user tries to add an empty task, an alert is displayed prompting them
to enter a task.