1722259436-Unit 3 - JavaScript and DOM Manipulation
1722259436-Unit 3 - JavaScript and DOM Manipulation
Front-End Development
Tools, Technologies and
Frameworks
Unit – 3
JavaScript and DOM Manipulation
Disclaimer
The content is curated from
online/offline resources and used for
educational purpose only.
Learning Objectives
You will learn in this lesson:
Source : https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/What_is_JavaScript
Use Case of JavaScript
1.User Interaction
2. Render message
Source : https://www.javascripttutorial.net/
Use Case of JavaScript
Chat Notification
Message Encryption
Chat Interface Update
Source : https://www.guru99.com/images/JavaScript/javascript1_1.png
Why JavaScript
• JavaScript offers lots of flexibility.
• Mobile app development, desktop app development,
and game development.
• With JavaScript you can find tons of frameworks and
libraries already developed, which can be used
directly in web development.
Source : https://www.guru99.com/images/JavaScript/javascript1_1.png
Where is JavaScript is Used
JavaScript is used in various fields from the web to servers:
• Web Applications
• Mobile Applications
• Web-based Games
• Back-end Web Development
JavaScript Syntax
JavaScript provides 3 places to put the
JavaScript code:
• Within body tag,
• Within head tag and
• External JavaScript file
Source : https://sharplesson.com/wp-content/uploads/2019/10/javascript-syntax.png
JavaScript Example
Comments in JavaScript
Source : https://www.w3schools.com/js/js_comments.asp
JavaScript Variables
What is Variable?
Source : https://lh3.googleusercontent.com/-YXC3gtpMlko/X3HA5DHH6MI/AAAAAAAAB3Q/VYM81zAFldY-cItuj7GMYA0Xy7Fy0GWBgCLcBGAsYHQ/image.png
JavaScript Variables
What is Variable?
Source : https://blog.devgenius.io/understand-the-difference-between-var-let-and-const-in-javascript-fdf19e18efd0
JavaScript Variables
Scope difference of var and let declarations
JavaScript Variables
Difference of const against var and let declarations
JavaScript Variables
Declaring Multiple Variables at Once
These are the following rules for naming a
JavaScript variable:
• A variable name must start with a letter,
underscore (_), or dollar sign ($).
• A variable name cannot start with a number.
• A variable name can only contain alpha-numeric
characters (A-z, 0-9) and underscores.
• A variable name cannot contain spaces.
• A variable name cannot be a JavaScript keyword
or a JavaScript reserved word.
Source : https://miro.medium.com/max/600/1*o960btjPmdMY5468NIcaGA.jpeg
JavaScript Variables
Valid Invalid
myVariable 1variable
my_variable my@variable
$myVariable var
myVariable123 my variable
MYVARIABLE123 my-variable
Source : https://www.istockphoto.com/photos/tick-and-cross
JavaScript Variables
Hoisting
Source : https://images.techopedia.com/definition/term-image/3929/javascript-js
Types of Datatype
Primitive Datatypes
Data Type Description
The primitive data types are the lowest level of the String String is a textual content wrapped inside ' ' or " " or `
data value in JavaScript. The typeof operator can be ` (tick sign).
used with primitive data types to know the type of a Example: 'Hello World!', "This is a string", etc.
value. Number Number is a numeric value.
Example: 100, 4521983, etc.
BigInt BigInt is a numeric value in the arbitrary precision
format.
Example: 453889879865131n, 200n, etc.
Boolean Boolean is a logical data type that has only two
values, true or false.
Null A null value denotes an absence of value.
Example: var str = null;
Undefined undefined is the default value of a variable that has
not been assigned any value.
Example: In the variable declaration, var str;, there is
no value assigned to str. So, the type of str can be
check using typeof(str) which will return undefined.
Types of Datatype
Non-Primitive Datatypes
Data Type Description
The non-primitive data types contain some kind Object An object holds multiple values in terms of
of structure with primitive data. properties and methods.
Example:
var person = {
firstName: "James",
lastName: "Bond",
age: 15
};
Date Date object represents date & time including
days, months, years, hours, minutes, seconds
and milliseconds.
Example: var today = new Date("25 July 2021");
Source : https://www.bookofnetwork.com/images/javascript-images/JS_Type-of-Operators_17Sep16_1822.png
Lab Activity
Hands On
Lab 7 -
JavaScript Basics
Types of JavaScript Operators
Bitwise Operators
Operator Description Example
The bitwise operators perform bitwise
operations on operands. & Bitwise AND (10==20 & 20==33) =
false
Hands On
Lab 8 -
JavaScript Operators
Conditional Statement
JavaScript Types of 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
• if statement
• else statement
• else if statement
• switch statement
Source : https://www.geeksforgeeks.org/javascript-if-else/
Conditional Statement
If statement
Source : https://www.geeksforgeeks.org/javascript-if-else/
Conditional Statement
If statement - Example
Output :
Value of a is greater than 10
Source : https://www.geeksforgeeks.org/javascript-if-else/
Conditional Statement
If-else statement
Source : https://www.geeksforgeeks.org/javascript-if-else/
Conditional Statement
If...else Statement Example
Output :
a is even number
Source : https://www.geeksforgeeks.org/javascript-if-else/
Conditional Statement
If-else-if statement
Source : https://www.geeksforgeeks.org/decision-making-javaif-else-switch-break-continue-jump/
Conditional Statement
If...else if statement
if(expression1){
//content to be evaluated if expression1 is true
}
else if(expression2)
{ //content to be evaluated if expression2 is true
}
else if(expression3)
{ //content to be evaluated if expression3 is true
}
else{ //content to be evaluated if no expression is true }
Source : https://media.geeksforgeeks.org/wp-content/uploads/if-elseif.png
Conditional Statement
If...else if statement Example
Source : https://media.geeksforgeeks.org/wp-content/uploads/if-elseif.png
Conditional Statement
If...else if statement Example continued
Output
a is equal to 20
Source : https://media.geeksforgeeks.org/wp-content/uploads/if-elseif.png
Conditional Statement
JavaScript Switch
Source : https://media.geeksforgeeks.org/wp-content/uploads/switch.png
Conditional Statement
JavaScript Switch
Syntax:
switch(expression){
case value1:
code to be executed;
break;
case value2:
code to be executed;
break;
......
default:
Source : https://media.geeksforgeeks.org/wp-content/uploads/switch.png
Conditional Statement
JavaScript Switch
Example:
Source : https://media.geeksforgeeks.org/wp-content/uploads/switch.png
Lab Activity
Hands On
Lab 9 -
i. for loop
ii. while loop
iii. do-while loop
iv. for-in loop
Source : https://media.geeksforgeeks.org/wp-content/uploads/Loop1.png
Loop Control Statement
For loop
Syntax:
Source : https://media.geeksforgeeks.org/wp-content/uploads/loop2.png
Loop Control Statement
For loop
Example: Output:
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25
Loop Control Statement
While loop
Syntax:
while (condition)
{
code to be executed
}
Source : https://media.geeksforgeeks.org/wp-content/uploads/Loop1.png
Loop Control Statement
While loop
Example:
Output:
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25
Loop Control Statement
for-in loop
Syntax:
for (variableName in Object)
{
statement(s)
}
Source : https://media.geeksforgeeks.org/wp-content/uploads/loop2.png
Loop Control Statement
For in loop
Example:
Output:
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25
Source : https://media.geeksforgeeks.org/wp-content/uploads/loop2.png
Loop Control Statement
for-of loop
Syntax:
for (variable of iterable) {
// code block to be executed
}
Source : https://media.geeksforgeeks.org/wp-content/uploads/loop2.png
Loop Control Statement
For of loop
Example: Output:
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25
Loop Control Statement
do while loop
Syntax:
do
{
code to be executed
}while (condition);
Source : https://media.geeksforgeeks.org/wp-content/uploads/loop3.png
Loop Control Statement
Do while loop
Example: Output:
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25
Loop Control Statement
Nested Loops
You can nest different types of loops to achieve the
desired iteration
Source : https://media.geeksforgeeks.org/wp-content/uploads/loop3.png
Loop Control Statement
Nested for loop
Example: Output:
(1, 1)
(1, 2)
(2, 1)
(2, 2)
(3, 1)
(3, 2)
Source : https://media.geeksforgeeks.org/wp-content/uploads/loop2.png
Lab Activity
Hands On
Lab 10 -
Source : https://cdn.educba.com/academy/wp-content/uploads/2019/09/Arrays-in-JavaScript.png
JavaScript Array
JavaScript array literal
Syntax:
var arrayname=[value1,value2.....valueN];
Syntax:
var arrayname=new Array();
Source : https://cdn.educba.com/academy/wp-content/uploads/2019/09/Arrays-in-JavaScript.png
JavaScript Array
JavaScript array constructor
Here, you need to create instance of array by passing arguments in constructor so that we don't have to
provide value explicitly.
Example: Output:
Jai
Vijay
Smith
JavaScript Array
Built-In Methods for Array Manipulation
Source : https://cdn.educba.com/academy/wp-content/uploads/2019/09/Arrays-in-JavaScript.png
JavaScript Object
What is object
Source : https://dev.to/himanshudevgupta/javascript-most-important-thing-object-2hm1
JavaScript Object
Accessing and modifying object
Source : https://dev.to/himanshudevgupta/javascript-most-important-thing-object-2hm1
JavaScript Object
Object Methods
• Object.assign() - Copies properties from one or more source objects to a target object.
• Object.create() - Creates a new object with a specified prototype object and properties.
• Object.defineProperty() - Defines a new property directly on an object or modifies an existing property.
• Object.entries() - Returns an array of a given object's own enumerable property [key, value] pairs.
• Object.keys() - Returns an array of a given object's own enumerable property names.
• Object.values() - Returns an array of a given object's own enumerable property values.
• Object.freeze() - Freezes an object, preventing new properties from being added or existing properties from
being modified.
JavaScript Function
What is JavaScript Function
Source : https://i0.wp.com/blog.alexdevero.com/wp-content/uploads/2020/01/javascript-functions-all-you-need-to-know-pt.1.jpg
JavaScript Function
Declaring a Function
Syntax:
function nameOfFunction ()
// function body
}
JavaScript Function
Calling a Function
function nameOfFunction ()
// function body
Source : https://cdn.programiz.com/cdn/farfuture/NdxxeWlRfoHMPgdcWPkeVy1wN9MwAgoqoYqZkFQDMFQ/mtime:1591592059/sites/tutorial2program/files/javascript-
function-example1.png
JavaScript Function
Function Return
Source : https://cdn.programiz.com/cdn/farfuture/b4h4Zo5ZYxj-EyfQyao-J5TqbKEefFgqqusPGLWPFS0/mtime:1591786573/sites/tutorial2program/files/javascript-
return-statement.png
JavaScript Function
Function Parameters
A function can also be declared with parameters. A parameter is a value that is passed when declaring a function
Source : https://cdn.programiz.com/cdn/farfuture/oAZVf3IqOKOYj_aJ-IoYQvbJ2CB-B3y4HXSLXBUmYcY/mtime:1591592163/sites/tutorial2program/files/javascript-
function-with-parameter.png
JavaScript Function
Benefits of Using a Function
Source : https://i0.wp.com/blog.alexdevero.com/wp-content/uploads/2020/01/javascript-functions-all-you-need-to-know-pt.1.jpg
JavaScript Function
Function Expressions
Output
16
9
Source : https://i0.wp.com/blog.alexdevero.com/wp-content/uploads/2020/01/javascript-functions-all-you-need-to-know-pt.1.jpg
Lab Activity
Hands On
Lab 11 -
Classes: Modules:
Promises:
Arrow Functions: Standardized
Simplified syntax Improved handling
Concise syntax for system for
for object-oriented of asynchronous
writing functions. organizing and
programming. operations.
sharing code.
Spread Operator:
Let and Const: Rest Operator:
Spread spreads
Block-scoped Rest gathers
elements into
variable elements into an
individual
declarations. array.
components.
DOM Manipulation
What is DOM ?
Source : https://www.w3schools.com/js/js_htmldom.asp
DOM Manipulation
Accessing Elements
Source : https://blog.csdn.net/unruly02/article/details/125408954
DOM Manipulation
Modifying Elements
Source : https://www.freecodecamp.org/news/dom-manipulation-in-javascript/
Lab Activity
Hands On
Lab 12 -
Onload - occurs when a page is loaded into the browser (i.E., Opened)
DOM Manipulation
Introduction to Events, Events Handling
JavaScript Events Example
Source : https://www.scaler.com/topics/javascript/event-handling-in-javascript/
Lab Activity
Hands On
Lab 13 -
Answer: C
let is block-scoped, var is function-scoped
Quiz
2. What does the === operator do in JavaScript?
Answer: B
It compares values and types
Quiz
3. Which method can be used to select an element by its ID in
JavaScript?
A) getElementByClass()
B) getElementById()
C) querySelectorId()
D) getElementByName()
Answer: B
getElementById()
Quiz
4. What is the correct way to write a single line comment in JavaScript?
Answer: C
// This is a comment
Quiz
5. Which of the following is used to attach an event handler to an element
in the DOM?
A) addEventListener()
B) attachEvent()
C) bind()
D) onEventListener()
Answer: A
addEventListener()
References
• https://www.w3schools.com/
• https://www.tutorialspoint.com
• https://developer.mozilla.org/en-US/docs/Learn/JavaScript
• https://www.geeksforgeeks.org/javascript/
• https://www.freecodecamp.org
Thank you!