JavaScript Lecture Notes BCA-5
JavaScript Lecture Notes BCA-5
1. Introduction:
- It is primarily used for client-side web development but can also be used on the server-side
(Node.js).
JavaScript is a fundamental language for web development and offers numerous benefits that
enhance the functionality and interactivity of websites. Here's a concise explanation of why JavaScript
is widely used:
a) Client-Side Interactivity: JavaScript allows developers to create dynamic and interactive web
pages, enhancing user experience without requiring constant page reloads.
b) User Interface Enhancement: It enables the modification of page content and styles in
response to user actions, making web applications more responsive and user-friendly.
c) DOM Manipulation: JavaScript can manipulate the Document Object Model (DOM) to
change, add, or remove elements from a webpage dynamically, resulting in real-time updates.
d) Event Handling: JavaScript facilitates the handling of user interactions, like clicks and form
submissions, by attaching event listeners to elements on the page.
e) Browser Compatibility: JavaScript is supported by all modern web browsers, ensuring broad
compatibility and consistent behaviour across different platforms.
g) Rich Ecosystem: JavaScript boasts a vast ecosystem of libraries (e.g., React, Angular, Vue.js)
and frameworks that simplify complex tasks, accelerate development, and encourage code
organization.
h) Server-Side Development: Through Node.js, JavaScript can be used for server-side scripting,
enabling a unified language for both client and server development.
i) Cross-Platform Development: JavaScript's versatility extends beyond web browsers, allowing
developers to create mobile applications using tools like React Native and frameworks like
Electron for desktop apps.
j) Community and Resources: JavaScript has a large, active community, resulting in abundant
online resources, tutorials, and open-source projects to aid learning and development.
k) Fast Development: Its concise syntax and dynamic typing enable faster development cycles,
allowing developers to iterate quickly.
l) Real-Time Updates: JavaScript's capabilities are foundational for building real-time features
like chat applications, live updates, and collaborative tools.
n) Animations and Effects: It facilitates the creation of animations, transitions, and visual
effects, contributing to more engaging and visually appealing websites.
o) Data Manipulation: JavaScript can process and manipulate data on the client side, reducing
the need for constant server requests and enhancing performance.
In essence, JavaScript empowers developers to create dynamic, feature-rich, and interactive web
applications that cater to modern user expectations and technological advancements.
- Variables are used to store data values. They can be declared using `var`, `let`, or `const`.
- Common data types include numbers, strings, booleans, arrays, objects, and more.
- Example:
1. `var`:
- `var` was historically used to declare variables in JavaScript before the introduction of `let` and
`const`.
- Variables declared with `var` have function-level scope, meaning they are accessible throughout the
entire function they are declared in, even outside of loops and conditionals.
- Variables declared with `var` are hoisted, which means they are moved to the top of their scope
during the compilation phase.
- `var` allows variable redeclaration within the same scope, potentially leading to unintended
consequences.
2. `const`:
- `const` is used to declare variables that should not be reassigned after their initial assignment.
- Variables declared with `const` are block-scoped, meaning they are only accessible within the block
they are defined in (block refers to code enclosed in curly braces).
- `const` variables are not hoisted in the same way as `var` variables, so they must be declared before
they are used.
- Once a value is assigned to a `const` variable, that value cannot be changed or reassigned.
- Use `var` if you need variable hoisting or need to support older browsers, but be cautious due to its
less predictable scoping behavior.
- Use `const` for values that should remain constant throughout their scope. This helps prevent
unintentional reassignments and improves code readability.
- Favor `let` over `var` for block-scoped variables when you need reassignment, as `let` provides more
predictable scoping behavior.
Example:
// Using var
var x = 10;
if (true) {
var x = 20;
}
console.log(x); // Output: 20
// Using const
const y = 10;
if (true) {
console.log(y); // Output: 10
In modern JavaScript development, `let` and `const` are preferred over `var` due to their more
predictable scoping behavior and the added benefits they provide for maintaining code quality and
reducing bugs.
3. Operators:
- JavaScript supports various operators like arithmetic, comparison, logical, assignment, and more.
- Example:
let sum = 10 + 5;
4. Control Flow:
- Use `if`, `else if`, and `else` statements for conditional execution.
- Example:
} else {
5. Functions:
- Example:
function greet(name) {
6. Arrays:
- Example:
7. Objects:
- Objects store key-value pairs and are used to represent complex data structures.
- Example:
let person = {
firstName: "John",
lastName: "Doe",
age: 30
};
8. DOM Manipulation:
- The Document Object Model (DOM) represents the structure of a web page.
- JavaScript can be used to interact with and manipulate the DOM to change content and styles
dynamically.
- Example:
9. Events:
- JavaScript can respond to events triggered by the user, like clicks and keyboard input.
- Example:
button.addEventListener("click", function() {
alert("Button clicked!");
});
Statements in JavaScript
1. Expression Statements:
- The expression is evaluated, and its result may have side effects.
- Example:
let x = 5;
console.log(x * 2);
- Example:
- Example:
} else {
4. Loops:
- Example:
- Example:
function greet(name) {
- Example:
if (i === 5) {
console.log(i);
7. Return:
function add(a, b) {
return a + b;
- Example:
try {
} catch (error) {
console.error(error.message);
}
Programs in JavaScript
ADDITION OF TWO NO.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript</title>
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css"
rel="stylesheet">
<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min
.js"></script>
</head>
<lable>Result</lable></br>
</div>
</body>
<script type="text/javascript">
function result(){
var a=Number(document.getElementById("first").value);
var b=Number(document.getElementById("second").value);
var c=a+b;
document.getElementById("result").value=Number(c);
</script>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript</title>
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css"
rel="stylesheet">
<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min
.js"></script>
</head>
<lable>Result</lable></br>
</div>
</body>
<script type="text/javascript">
function result(){
var a=Number(document.getElementById("first").value);
var b=Number(document.getElementById("second").value);
var c=a*b;
document.getElementById("result").value=Number(c);
</script>
</html>
SIMPLE INTEREST
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple Interest</title>
<style type="text/css">
#container{
width: 50%;
margin: 8px 0;
display: inline-block;
border-radius: 4px;
box-sizing: border-box;
div {
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
input[type=Submit] {
width: 100%;
background-color: #4CAF50;
color: white;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
input[type=text], select {
width: 100%;
margin: 8px 0;
display: inline-block;
border-radius: 4px;
box-sizing: border-box;
input[type=Submit]:hover {
background-color: #45a049;
input[type=Reset]:hover {
background-color: #45a049;
</style>
</head>
<div id="container">
</br>
confirm("Enter OK to Continue");
function result()
var p=Number(document.getElementById("principal").value);
var r=Number(document.getElementById("interest").value);
var t=Number(document.getElementById("time").value);
var simpleint=p*r*t/100;
document.getElementById("simpleint").value=Number(simpleint);
function reset()
document.getElementById("principal").value="0";
document.getElementById("interest").value="0";
document.getElementById("time").value="0";
document.getElementById("simpleint").value="0";
</script>
</body>
</html>