JavaScript Basics: Interactive Web Programming
JavaScript Basics: Interactive Web Programming
A “Hello, World!” program is the simplest way to get started with any
programming language. Here’s how you can write one using JavaScript.
<html>
<head></head>
<body>
<h1>Check the console for the message!</h1>
<script>
// This is our first JavaScript program
[Link]("Hello, World from console..!");
alert("Hello, World from alert..!");
</script>
</body>
</html>
In this example
We can also print the “Hello World” program directly into the console
terminal without embedded it into HTML. Create an [Link] file and add the
code to it.
In this example
Applications of JavaScript
JavaScript Syntax
JavaScript syntax refers to the rules and conventions dictating how code is
structured and arranged within the JavaScript programming language. This
includes statements, expressions, variables, functions, operators and control
flow constructs.
In coding, “syntax” refers to the set of rules that defines the structure and
format of the code in a programming language. It dictates how code should
be written so that it can be correctly interpreted and executed by the
compiler or interpreter.
JavaScript code is embedded in HTML using the <script> tag. The script can
be added using several methods i.e.
1. Inline JavaScript
Writing JavaScript code directly inside the HTML element using the onclick,
onmouseover, or other event handler attributes. E.g.
<html>
<head></head>
<body>
<h2> Adding JavaScript in HTML Document </h2>
<button onclick="alert('JavaScript Button Clicked..!')"> Click Here
</button>
</body>
</html>
You can write JavaScript code inside the <script> tag within the HTML file.
This is known as internal JavaScript and is commonly placed inside the
<head> or <body> section of the HTML document.
<html>
<head>
<script>
function myFun() {
[Link]("demo")
.innerHTML = "This content is from JavaScript Code Inside
head Tag..!";
}
</script>
</head>
<body>
<h2>Add JavaScript Code inside Head Section </h2>
<h3 id="demo" style="color:green;"> To demostrate JavaScript
Code Inside head Tag </h3>
<button type="button" onclick="myFun()"> Click Here </button>
</body>
</html>
<html>
<head></head>
<body>
<h2> Add JavaScript Code inside Body Section </h2>
<h3 id="demo" style="color:green;"> To demostrate JavaScript Code
Inside body Tag </h3>
<button type="button" onclick="myFun()"> Click Here </button>
<script>
function myFun() {
[Link]("demo")
.innerHTML = " This content is from JavaScript Code Inside body
Tag..!";
}
</script>
</body>
</html>
For larger projects or when reusing scripts across multiple HTML files, you
can place your JavaScript code in an external .js file. This file is then linked to
your HTML document using the src attribute within a <script> tag.
HTML:
<html>
<head>
<script src="[Link]"></script>
</head>
<body>
<h2> External JavaScript </h2>
<h3 id="demo" style="color:green;"> To demonstrate External
JavaScript </h3>
<button type="button" onclick="myFun()"> Click Here </button>
</body>
</html>
JavaScript:
/* Filename: [Link]*/
function myFun () {
[Link]('demo')
Faster Page Load Times: Cached external JavaScript files don’t need
to be reloaded every time the page is visited, which can speed up
loading times.
1. async Attribute
This attribute loads the script asynchronously, i.e. the script will be
downloaded and executed as soon as it is available, without blocking the
page.
<script src="[Link]" async></script>
2. defer Attribute
This attribute delays the execution of the script until the entire HTML
document has been parsed. This is particularly useful for scripts that
manipulate the DOM.
<script src="[Link]" defer></script>
Referencing External JavaScript Files
There are three ways to reference an external script in JavaScript:
By using a full URL: src = "[Link]
By using a file path: src = "/js/[Link]"
Without using any path: src = "[Link]"
JavaScript Output:
1. Using innerHTML
<!DOCTYPE html>
<html>
<body>
<h2>Inner HTML output</h2>
<p id="demo"></p>
<script>
[Link]("demo").innerHTML = 5 + 6;
</script>
</body>
</html>
2. Using [Link]()
<!DOCTYPE html>
<html>
<body>
<h2>Using [Link]</h2>
<script> [Link](5 + 6); </script>
</body>
</html>
Note: Using [Link]() after an HTML document is loaded, will delete
all existing HTML. Thus [Link]() method should only be used for
testing. E.g.
<!DOCTYPE html>
<html>
<body>
<h2>Using [Link] - deletes html </h2>
<p>Other html data that will be deleted also </p>
<button type="button" onclick="[Link](5 + 6)">Click Here
</button>
</body>
</html>
3. Using [Link]()
<!DOCTYPE html>
<html>
<body>
<h2>Using windows alert </h2>
<script>
[Link](5 + 6);
//alert(5 + 6);
</script>
</body>
</html>
You can skip the window keyword as shown under the commented line.
The window object is the global scope object. This means that variables,
properties, and methods by default belong to the window object. This
also means that specifying the window keyword is optional.
4. Using [Link]()
For debugging purposes, you can call the [Link]() method in the
browser to display data.
<!DOCTYPE html>
<html>
<body>
</body>
</html>
JavaScript Print
JavaScript does not have any print object or print methods; thus you cannot
access output devices from JavaScript.
The only exception is that you can call the [Link]() method in the
browser to print the content of the current window.
<!DOCTYPE html>
<html>
<body>
<h2>Using windows print function: </h2>
<p>This will print the <b> current page </b> to either device, pdf
format etc.</p>
<button onclick="[Link]()">Print this page</button>
</body>
</html>
JavaScript Statements/code
JavaScript statements are programming instructions that a computer
executes. They are composed of:
Values
Operators
Expressions
Keywords
Comments
This statement tells the browser to write a given data/ statement inside an
HTML element id. E.g.
[Link]("demo").innerHTML = "Internet application
Programming.";
The statements are executed, one by one, in the same order as they are
written.
Keyword Description
var Declares a variable
let Declares a block variable
const Declares a block constant
if Marks a block of statements to be executed on a condition
switch Marks a block of statements to be executed in different cases
for Marks a block of statements to be executed in a loop
function Declares a function
return Exits a function
try Implements error handling to a block of statements
Note: Reserved words cannot be used as names for variables.
Giving examples, discuss various types of JavaScript statements?
JavaScript Events:
<html>
<script>
function myFun() {
[Link](
"iap").innerHTML = "Click event triggered, for Internet
Application Programming";
}
</script>
<body>
<button onclick="myFun()">Click me </button>
<p id="iap"></p>
</body>
</html>
The onclick attribute in the <button> calls the myFun() function
when clicked.
Initially, the <p> is empty, and its content changes dynamically on the
button click.
Event Types:
Event Description
Attribute
onclick Triggered when an element is clicked.
onmouseover Fired when the mouse pointer moves over an
element.
onmouseout Occurs when the mouse pointer leaves an
element.
onkeydown Fired when a key is pressed down.
onkeyup Fired when a key is released.
onchange Triggered when the value of an input element
changes.
onload Occurs when a page has finished loading.
onsubmit Fired when a form is submitted.
onfocus Occurs when an element gets focus.
onblur Fired when an element loses focus.
<html>
<body>
<h2> Inline HTML Handlers </h2>
<button onclick="alert('Button clicked!')">Click Me</button>
</body>
</html>
2. DOM Property Handlers
3. addEventListener() (Preferred)
[Link]("click", () => {
alert("Button clicked using addEventListener!");
});
Practical Applications
1. Form Validation
<html>
<body>
<h2>Form Validation</h2>
<form id="formval">
<input type="text" placeholder="Enter some values"
id="formInput" />
<button type="submit">Submit</button>
</form>
<script>
[Link]("#formval").addEventListener("submit",
(e) => {
let input = [Link]("#formInput");
if (![Link]) {
[Link]();
alert("Input cannot be empty");
}
else{
alert("Something inserted into database.");
}
});
</script>
</body>
</html>
JavaScript Variables