UNIT-IV Webtechnology
UNIT-IV Webtechnology
JAVASCRIPT
HOW TO ADD SCRIPT TO YOUR PAGES:
What Is JavaScript?
JavaScript was designed to add interactivity to HTML pages
JavaScript is a scripting language
A scripting language is a lightweight programming language
JavaScript is usually embedded directly into HTML pages
JavaScript is an interpreted language (means that scripts execute without
preliminary compilation)
Everyone can use JavaScript without purchasing a license
Linking JavaScript with HTML
There are following three main ways to add JavaScript to your web pages:
1. Embedding code: Adding the JavaScript code between a pair of
<script>...</script> tag
2.Inline code: Placing JavaScript code directly inside HTML tags using some
special attributes.
3.External file: Making a separate JavaScript file having .js as an extension.
Inline JavaScript
Inline JavaScript is placed directly within <script> tags in the HTML document.
This method is useful for small scripts or scripts specific to a single page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Inline JavaScript Example</title>
<!-- No external script, just inline -->
<script>
function sayHello() {
alert('Hello, world!');
}
</script>
</head>
<body>
<h1>Inline JavaScript Example</h1>
External JavaScript
For larger scripts or reusable code, it’s common practice to create an external
JavaScript file and link to it using the src attribute of the <script> tag. This keeps
your HTML cleaner and separates content from behavior.
// script.js
function sayHello() {
alert('Hello, world!');
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>External JavaScript Example</title>
<!-- Link to external script -->
<script src="script.js"></script>
</head>
<body>
<h1>External JavaScript Example</h1>
Output
Hello
10
12
22
let Keyword
The let keyword is a block-scoped variables. It’s commonly used for variables that
may change their value.
let y = "Hello";
Example:
Output
Hello learners
joining
12
joining 12
const Keyword
The const keyword declares variables that cannot be reassigned. It’s block-scoped
as well.
const PI = 3.14;
Example:
const b = 400;
console.log(b);
const c = "12";
console.log(c);
Output
Hello learners
400
12
Data Types
JavaScript is a dynamically typed (also called loosely typed) scripting language.
In JavaScript, variables can receive different data types over time. The latest
ECMAScript standard defines eight data types Out of which seven data types are
Primitive (predefined) and one complex or Non-Primitive.
Primitive Data Types
The predefined data types provided by JavaScript language are known as primitive
data types. Primitive data types are also known as in-built data types.
Number: JavaScript numbers are always stored in double-precision 64-bit
binary format IEEE 754. Unlike other programming languages, you don’t need
int, float, etc to declare different numeric values.
String: JavaScript Strings are similar to sentences. They are made up of a list of
characters, which is essentially just an “array of characters, like “Hello world”
etc.
Boolean: Represent a logical entity and can have two values: true or false.
Null: This type has only one value that is null.
Undefined: A variable that has not been assigned a value is undefined.
Symbol: Symbols return unique identifiers that can be used to add unique
property keys to an object that won’t collide with keys of any other code that
might add to the object.
BigInt: BigInt is a built-in object in JavaScript that provides a way to represent
whole numbers larger than 2^53-1.
The data types that are derived from primitive data types of the JavaScript
language are known as non-primitive data types. It is also known as derived data
types or reference data types.
Object: It is the most important data type and forms the building blocks for
modern JavaScript. We will learn about these data types in detail in further
articles.
STATEMENTS AND OPERTORS:
Operator:
In JavaScript, an operator is a symbol that performs an operation on one or more
operands, such as variables or values, and returns a result. Let us take a simple
expression 4 + 5 is equal to 9. Here 4 and 5 are called operands, and ‘+’ is called
the operator.
Arithmetic Operators
Comparison Operators
Logical (or Relational) Operators
Bitwise Operators
Assignment Operators
Miscellaneous Operators
Arithmetic Operators
Comparison Operators
The JavaScript comparison operators compare two values and returns a boolean
result (true or false). JavaScript supports the following comparison operators −
The logical operators are generally used to perform logical operations on boolean
values. But logical operators can be applied to values of any types not only boolean.
Assignment Operators
Miscellaneous Operators
There are few other operators supported by JavaScript. These operators are
conditional operator (? :), typeof operator, delete operator, etc.
In the below table, we have given the JavaScript miscellaneous operators with its
explanation.
Operator Description
? : (Conditional ) If Condition is true? Then value X : Otherwise value Y
typeof It returns the data type of the operand.
?? (Nullish Coalescing It returns its right-hand side operand when its left-hand
Operator) side operand is null or undefined, and otherwise returns
its left-hand side operand.
delete It removes a property from an object.
, (Comma) It evaluates its operands (from left to right) and returns
the value of the last operand.
() (Grouping) It allows to change the operator precedence.
yield It is used to pause and resume a generator function.
… (Spread) It is used to expand the iterables such as array or string.
** (Exponentiation) Raises the left operand to the power of the right operand
STATEMENTS:
In JavaScript, statements are instructions that are executed by the browser or Node.js.
A statement can be a simple or complex operation that performs an action, such as
assigning a value to a variable, calling a function, or controlling program flow with
conditional statements.
Declaration Statements
Declaration statements are variables, functions, or classes that are introduced into a
program. These statements begin with a keyword followed by its identifier or name.
Variables are containers for storing data values:
var x = 5;
var y = 6;
var X = 4;
var z = x + y;
var x = 5; Output:
console.log(x) 5
var y = 6; 6
console.log(y) 4
var x = 4; 10
console.log(x)
var z = x + y;
console.log(z)
function add(a, b) {
return a + b;
}
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
switch statement: Allows you to execute one block of code among many
alternatives.
Conditional Statements:
JavaScript conditional statements allow you to execute specific blocks of code
based on conditions. If the condition is met, a particular block of code will run;
otherwise, another block of code will execute based on the condition.
There are several methods that can be used to perform Conditional Statements in
JavaScript.
Conditional Description
Statement
if statement Executes a block of code if a specified condition is true.
else statement Executes a block of code if the same condition of the
preceding if statement is false.
else if statement Adds more conditions to the if statement, allowing for multiple
alternative conditions to be tested.
switch Evaluates an expression, then executes the case statement that
statement matches the expression’s value.
ternary operator Provides a concise way to write if-else statements in a single
line.
Nested if else Allows for multiple conditions to be checked in a hierarchical
statement manner.
Loop Statements:
let i = 0;
while (i < 5) {
console.log(i); // Outputs 0, 1, 2, 3, 4
i++;
}
do...while loop: Similar to a while loop, but it always executes the block of
code once before checking the condition.
let i = 0;
do {
console.log(i); // Outputs 0, 1, 2, 3, 4
i++;
} while (i < 5);
FUNCTIONS IN JAVASCRIPT:
Function Declaration
Functions in JavaScript can be declared using the function keyword. There are
several ways to declare functions:
1. Function Declaration:
function greet(name) {
return `Hello, ${name}!`; }
This is a named function (greet), which takes a parameter name and returns a
greeting string.
2. Function Expression:
Calling Functions
Functions can take parameters (values passed to the function) and return values
(values returned by the function after execution):
function add(a, b) {
return a + b;
}
Function Scope
Variables declared inside a function are scoped to that function (local scope),
unless declared with var (which would make them function-scoped), let, or
const (block-scoped):
function example() {
const localVar = "Hello";
console.log(localVar); // Output: Hello
}
Function Hoisting
In JavaScript, function declarations are hoisted, meaning they are moved to the top
of their scope during compilation, allowing you to call functions before they're
declared in the code:
Callback Functions
function fetchData(callback) {
setTimeout(() => {
const data = "Some fetched data";
callback(data);
}, 1000);
}
fetchData((data) => {
console.log(data); // Output after 1 second: Some fetched data
});
Function Return
When JavaScript reaches a return statement, the function will stop executing.
If the function was invoked from a statement, JavaScript will "return" to execute
the code after the invoking statement.
Functions often compute a return value. The return value is "returned" back to the
"caller":
Example Calculate the product of two numbers, and return the result:
function myFunction(a, b) {
// Function returns the product of a and b
return a * b;
}
Message Box
Dialog Box:
In JavaScript, "dialog boxes" refer to popup windows that interact with users by
displaying messages, asking for confirmation, or prompting for input. There are
several types of dialog boxes commonly used:
Alert Box
An alert box is often used if you want to make sure information comes through to
the user.
When an alert box pops up, the user will have to click "OK" to proceed.
Syntax
window.alert("hello world");
Example
Confirm Box:
A confirm box is often used if you want the user to verify or accept something.
When a confirm box pops up, the user will have to click either "OK" or "Cancel"
to proceed.
If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box
returns false.
Syntax
window.confirm("Are you okay");
Example
if (confirm("Press a button!")) {
txt = "You pressed OK!";
} else {
txt = "You pressed Cancel!";
}
Prompt Box:
A prompt box is often used if you want the user to input a value before entering a
page.
When a prompt box pops up, the user will have to click either "OK" or "Cancel" to
proceed after entering an input value.
If the user clicks "OK" the box returns the input value. If the user clicks "Cancel"
the box returns null.
Syntax
window.prompt("Are you okay"," Yes I’m Good");
Example