Unit 4 UGCSA104
Unit 4 UGCSA104
LECTURE NOTES
Unit IV
Working with Java Script (6L)
CONTENT:
1. Introduction to JavaScript
2. Data types
3. Variables
4. Operators
5. Expressions
6. Statements
7. Functions
8. Objects
9. Arrays
10. Date
11. Math
12. Error Handling
13. Flow control
14. Loops
15. Document Object Model (DOM).
Introduction to JavaScript:
Versatile Usage: Apart from web development, JavaScript is now used in various
environments, including server-side development (Node.js), mobile app
development, and game development.
JavaScript has several data types that are classified into two categories: primitive
and object.
let person = {
name: 'John',
age: 25,
isStudent: true
};
Variables in JavaScript:
Variables are used to store and manipulate data in JavaScript. To declare a variable,
you can use the let, const, or var keyword.
var is used for declaring variables, but it has some differences in behavior
compared to let and const.
Notes By:- Pratik Pandey 3
Assistant Professor
VIVEKANANDA GLOBAL UNIVERSITY
LECTURE NOTES
var counter = 0;
Variables in JavaScript:
Variables in JavaScript are used to store and manage data. They are declared using
the let, const, or var keyword, and their values can be updated or reassigned.
let age = 25; // Declaring a variable 'age' and assigning the value 25
const pi = 3.14; // Declaring a constant 'pi' with the value 3.14
var name = "John"; // Older way of declaring a variable
Operators in JavaScript:
Operators perform operations on variables and values. JavaScript has various types
of operators, including:
Arithmetic Operators:
Notes By:- Pratik Pandey 4
Assistant Professor
VIVEKANANDA GLOBAL UNIVERSITY
LECTURE NOTES
let a = 10;
let b = 5;
Comparison Operators:
let x = 10;
let y = 5;
Logical Operators:
let p = true;
let q = false;
Assignment Operators:
Unary Operators:
let count = 5;
count++; // Increment by 1 (count = count + 1)
count--; // Decrement by 1 (count = count - 1)
Expressions in JavaScript:
Statements in JavaScript:
Declaration Statements:
Expression Statements:
Notes By:- Pratik Pandey 6
Assistant Professor
VIVEKANANDA GLOBAL UNIVERSITY
LECTURE NOTES
Statements that include expressions.
Used for control flow in the program, such as if, else, switch, for, while, etc.
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
Functions in JavaScript:
Functions are reusable blocks of code that can be defined and called with a set of
parameters. They encapsulate a specific task or behavior.
// Function declaration
function greet(name) {
console.log(`Hello, ${name}!`);
}
// Function call
greet("John"); // Output: Hello, John!
Objects in JavaScript:
Objects in JavaScript are complex data types that store data in key-value pairs.
They can represent real-world entities and have properties and methods.
// Object literal
let person = {
name: "John",
age: 30,
greet: function() {
console.log("Hello!");
}
};
Objects provide a way to organize and structure data, and they can be used to
model various entities in a program.
Arrays in JavaScript:
// Array literal
let fruits = ["apple", "banana", "orange"];
Arrays are versatile and can hold various data types. They provide methods for
manipulating and iterating through their elements.
The Date object in JavaScript is used to work with dates and times. It provides
methods for creating, formatting, and manipulating dates.
The Date object also supports various methods for working with time, such as
getHours(), getMinutes(), getSeconds(), etc.
// Rounding a number
let roundedNum = Math.round(num); // Rounds to the nearest integer
The Math object includes functions for common mathematical operations like
round(), floor(), ceil(), max(), and min().
Error handling in JavaScript is done using try, catch, finally, and throw statements.
try {
// Code that may throw an error
let result = x / y;
if (isNaN(result)) {
throw new Error("Result is not a number!");
}
// Continue with normal execution if no error occurs
} catch (error) {
Notes By:- Pratik Pandey 10
Assistant Professor
VIVEKANANDA GLOBAL UNIVERSITY
LECTURE NOTES
// Code to handle the error
console.error("An error occurred:", error.message);
} finally {
// Code that always executes, whether an error occurred or not
}
The try block contains the code that might throw an error. If an error occurs, it is
caught and handled in the catch block. The finally block is optional and executes
regardless of whether an error occurred or not.
// For loop
for (let i = 0; i < 5; i++) {
console.log(i);
Notes By:- Pratik Pandey 11
Assistant Professor
VIVEKANANDA GLOBAL UNIVERSITY
LECTURE NOTES
}
// While loop
let counter = 0;
while (counter < 5) {
console.log(counter);
counter++;
}
// Do-while loop
let index = 0;
do {
console.log(index);
index++;
} while (index < 5);
These control flow structures help manage the execution flow of a program based
on conditions and repetitions. They are essential for writing flexible and dynamic
JavaScript code.
Loops in JavaScript:
Loops are used to execute a block of code repeatedly. JavaScript provides several
types of loops, including for, while, and do-while.
1. For Loop:
let counter = 0;
3. Do-While Loop:
let index = 0;
do {
console.log(index);
index++;
} while (index < 5);
The do-while loop is similar to the while loop, but it guarantees that the block of
code is executed at least once, as the condition is checked after the block.
<script>
// JavaScript code to manipulate the DOM
function changeText() {
// Accessing an element by its ID
let heading = document.getElementById("myHeading");
</body>
</html>
In this example:
The HTML document contains an <h1> element with the ID "myHeading" and a
<button> element.
The JavaScript code defines a function changeText() that manipulates the content
of the <h1> element when the button is clicked.
Use properties like textContent, innerHTML, style, etc., to change the content or
style of the selected elements.
Respond to Events:
Use event listeners to respond to user interactions (e.g., clicks, key presses) and
trigger JavaScript functions.
DOM manipulation allows dynamic updates to web pages, making them interactive
and responsive to user actions. It's a key aspect of creating modern web
applications.