Skip to main content
Prepared By: Kale J. N.
JavaScript Control Structures
JavaScript Control Structures are used to control the flow of execution in a program based on
conditions or loops.
1. Conditional Statements (Decision Making)
1.1 if Statement
Executes a block if the condition is true.
Example:
let age = 20;
if (age >= 18) {
console.log("Eligible to vote");
}
1.2 if...else Statement
Executes one block if true, another if false.
Example:
let age = 16;
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}
1.3 if...else if...else
Used when multiple conditions exist.
Example:
let marks = 75;
if (marks >= 90) {
console.log("Grade A");
} else if (marks >= 70) {
console.log("Grade B");
} else if (marks >= 50) {
console.log("Grade C");
} else {
console.log("Fail");
}
Prepared By: Kale J. N.
July 17, 2026 | Page 1 of 18
Prepared By: Kale J. N.
1.4 switch Statement
Used when comparing a variable with multiple values.
Example:
let day = 2;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
default:
console.log("Invalid day");
}
2. Looping Statements (Repetition)
2.1 for Loop
Used when number of iterations is known.
Example:
for (let i = 1; i <= 5; i++) {
console.log(i);
}
2.2 while Loop
Runs while condition is true.
Example:
let i = 1;
while (i <= 5) {
console.log(i);
i++;
}
2.3 do...while Loop
Executes at least once.
Example:
Prepared By: Kale J. N.
July 17, 2026 | Page 2 of 18
Prepared By: Kale J. N.
let i = 1;
do {
console.log(i);
i++;
} while (i <= 5);
2.4 for...in Loop
Used to iterate over object properties.
Example:
const person = {
name: "ABC",
age: 25
};
for (let key in person) {
console.log(key + " : " + person[key]);
}
2.5 for...of Loop
Used to iterate over iterable objects.
Example:
let colors = ["red", "green", "blue"];
for (let color of colors) {
console.log(color);
}
3. Jump Statements
3.1 break
Terminates loop immediately.
Example:
for (let i = 1; i <= 10; i++) {
if (i === 5) break;
console.log(i);
}
3.2 continue
Skips current iteration.
Example:
for (let i = 1; i <= 5; i++) {
if (i === 3) continue;
Prepared By: Kale J. N.
July 17, 2026 | Page 3 of 18
Prepared By: Kale J. N.
console.log(i);
}
3.3 return
Used inside functions to exit and send value back.
Example:
function add(a, b) {
return a + b;
}
Prepared By: Kale J. N.
July 17, 2026 | Page 4 of 18
Prepared By: Kale J. N.
Arrays in Java Script
1. What is an Array?
An array is a special variable that can hold multiple values.
Example:
let numbers = [10, 20, 30, 40];
2. Creating Arrays
Method 1: Array Literal
let fruits = ["Apple", "Banana", "Mango"];
Method 2: Using new Array()
let nums = new Array(1, 2, 3, 4);
3. Accessing Elements
let arr = ["A", "B", "C"];
arr[0];
arr[arr.length - 1];
4. Array Property
length
let arr = [1,2,3];
arr.length; // 3
5. Modifying Arrays
push() - Add at end
unshift() - Add at beginning
pop() - Remove from end
shift() - Remove from beginning
6. Looping Through Arrays
For Loop
for(let i = 0; i < arr.length; i++){
console.log(arr[i]);
}
for...of
for(let value of arr){
console.log(value);
}
Prepared By: Kale J. N.
July 17, 2026 | Page 5 of 18
Prepared By: Kale J. N.
forEach()
arr.forEach(function(item){
console.log(item);
});
7. Important Array Methods
map() - Creates new array
It is an array method that:
 Iterates over each element
 Applies a function to each element
 Returns a new array
 Does NOT modify the original array
filter() - Filters elements
filter() is an array method that:
 Iterates over each element
 Checks a condition
 Returns a new array with only elements that satisfy the condition
 Does NOT modify the original array
reduce() - Reduces to single value
reduce() is an array method that:
 Iterates over each element
 Combines all elements into one single value
 Returns a single output (number, object, array, string, etc.)
 Does NOT modify the original array
 It is used for sum, product, grouping, counting, flattening, etc.
sort() - Sorts array
reverse() - Reverses array
Prepared By: Kale J. N.
July 17, 2026 | Page 6 of 18
Prepared By: Kale J. N.
8. Spread Operator
The spread operator […] is used to expand elements of an array into individual values. It
helps in copying arrays, merging arrays, and adding new elements without modifying the
original array.
Example:
let arr2 = [...arr1, 4, 5]; → Creates a new array by copying arr1 elements and adding 4 and 5.
9. Destructuring
Destructuring is a feature in JavaScript that allows you to extract values from arrays or
objects and assign them to variables in a simple way.
let [a, b] = [10, 20];
10. Important Concepts
- Arrays are objects
- Dynamic size
- Can store mixed data types
11. Shallow vs Deep Copy
Shallow Copy:
let arr2 = arr1;
This does not create a new array.
It only copies the reference (memory address).
Both arr1 and arr2 point to the same array.
Proper Copy:
let arr2 = [...arr1];
This creates a new array and copies elements.
Prepared By: Kale J. N.
July 17, 2026 | Page 7 of 18
Prepared By: Kale J. N.
JavaScript Objects
1. Introduction to Objects
An object in JavaScript is a collection of key–value pairs.
• Keys → called properties
• Values → can be any data type (string, number, array, function, another object, etc.)
Objects are used to represent real-world entities.
Example:
const person = {
name: "abc",
age: 25,
isStudent: true
};
Here:
• name, age, isStudent → properties
• "abc", 25, true → values
2. Creating Objects
2.1 Object Literal (Most Common Method)
const car = {
brand: "Toyota",
model: "Innova",
year: 2023
};
2.2 Using new Object()
const car = new Object();
car.brand = "Toyota";
car.model = "Innova";
car.year = 2023;
Prepared By: Kale J. N.
July 17, 2026 | Page 8 of 18
Prepared By: Kale J. N.
2.3 Using Constructor Function
function Person(name, age) {
this.name = name;
this.age = age;
}
const p1 = new Person("Alice", 30);
2.4 Using Class (Modern Method – ES6)
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
const p1 = new Person("Alice", 30);
3️. Accessing Object Properties
3.1 Dot Notation
console.log(person.name);
//prints the value of the name property of the person object to the console.
3.2 Bracket Notation
console.log(person["age"]);
Bracket notation is useful when:
 Property name is dynamic
 Property name has spaces
Example:
const user = {
"first name": "Rahul"
};
console.log(user["first name"]);
Prepared By: Kale J. N.
July 17, 2026 | Page 9 of 18
Prepared By: Kale J. N.
4️. Adding, Updating & Deleting Properties
4.1 Add Property
person.city = "Mumbai";
//adds a new property city to the person object (or updates it) with the value "Mumbai".
4.2 Update Property
person.age = 26;
4.3 Delete Property
delete person.isStudent;
//removes the isStudent property from the person object.
5️. Object Methods
When a function is stored inside an object, it is called a method.
const person = {
name: "John",
greet: function() {
console.log("Hello!");
}
};
person.greet();
6️. Looping Through Objects
6.1 for...in Loop
const person = {
name: "John",
age: 25
};
for (let key in person) {
console.log(key + ": " + person[key]);
Prepared By: Kale J. N.
July 17, 2026 | Page 10 of 18
Prepared By: Kale J. N.
}
6.2 Object Methods
Object.keys()
console.log(Object.keys(person));
//prints an array of all property names (keys) of the person object.
Object.values()
console.log(Object.values(person));
//prints an array of all property values of the person object.
Object.entries()
console.log(Object.entries(person));
//prints an array of [key, value] pairs for each property in the person object.
7️. Nested Objects
Objects inside another object.
const student = {
name: "Amit",
marks: {
math: 90,
science: 85
}
};
console.log(student.marks.math);
8️. Object Destructuring (ES6)
Extract properties into variables.
const person = {
name: "John",
age: 25
};
const { name, age } = person;
console.log(name);
Prepared By: Kale J. N.
July 17, 2026 | Page 11 of 18
Prepared By: Kale J. N.
9️. Spread Operator in Objects
Used to copy or merge objects.
const obj1 = { a: 1 };
const obj2 = { b: 2 };
const obj3 = { ...obj1, ...obj2 };
10. JSON and Objects
Convert Object to JSON
const jsonString = JSON.stringify(person);
//converts the person object into a JSON string.
Convert JSON to Object
const obj = JSON.parse(jsonString);
//converts the JSON string jsonString back into a JavaScript object.
Prepared By: Kale J. N.
July 17, 2026 | Page 12 of 18
Prepared By: Kale J. N.
DOM Manipulation in JavaScript
1️. What is DOM?
DOM (Document Object Model) is a programming interface for HTML and XML documents.
• It represents the web page as a tree structure
• Each element becomes an object (node)
• JavaScript can access, modify, add, delete elements dynamically
Browser creates DOM when HTML page loads.
Example HTML:
<html>
<head> </head>
<body>
<h1>Hello</h1>
</body>
</html>
DOM Tree:
Prepared By: Kale J. N.
July 17, 2026 | Page 13 of 18
Prepared By: Kale J. N.
2️. Selecting Elements
2.1 getElementById()
let element = document.getElementById("title");
✔ Returns single element
2.2 getElementsByClassName()
let elements = document.getElementsByClassName("box");
✔ Returns HTMLCollection (array-like)
2.3 getElementsByTagName()
let elements = document.getElementsByTagName("p");
2.4 querySelector()
let element = document.querySelector("#title");
✔ Returns first matching element
2.5 querySelectorAll()
let elements = document.querySelectorAll(".box");
✔ Returns NodeList
3️. Changing Content
Prepared By: Kale J. N.
July 17, 2026 | Page 14 of 18
Prepared By: Kale J. N.
3.1 innerHTML
element.innerHTML = "<b>Hello</b>";
sets the HTML content of the selected element to bold "Hello", rendering it as formatted HTML
instead of plain text.
3.2 textContent
element.textContent = "Hello";
sets the element’s content to plain text "Hello" without interpreting any HTML tags.
3.3 innerText
element.innerText = "Hello";
sets the visible text of the element to "Hello", respecting CSS styles and ignoring hidden content.
4️. Changing Attributes
4.1 getAttribute()
element.getAttribute("href");
returns the value of the href attribute of the selected element.
4.2 setAttribute()
element.setAttribute("href", "https://example.com");
sets or updates the element’s href attribute to "https://example.com"
4.3 removeAttribute()
element.removeAttribute("disabled");
removes the disabled attribute from the element, enabling it if it was previously disabled.
Prepared By: Kale J. N.
July 17, 2026 | Page 15 of 18
Prepared By: Kale J. N.
5️. Manipulating Styles
5.1 Inline Style
element.style.color = "red";
element.style.backgroundColor = "yellow";
5.2 Adding / Removing Classes
element.classList.add("active");
adds the "active" CSS class to the selected element.
element.classList.remove("active");
removes the "active" CSS class from the selected element.
element.classList.toggle("active");
adds the "active" class if it’s not present, and removes it if it is present.
element.classList.contains("active");
checks whether the element has the "active" class and returns true or false.
6️. Creating & Adding Elements
6.1 createElement()
let newDiv = document.createElement("div");
creates a new <div> element in memory (not yet added to the page) and stores it in the variable newDiv.
6.2 appendChild()
document.body.appendChild(newDiv);
adds the newDiv element as the last child inside the <body> of the document.
6.3 append()
parent.append(child);
adds the child element as the last child of the parent element.
Prepared By: Kale J. N.
July 17, 2026 | Page 16 of 18
Prepared By: Kale J. N.
6.4 prepend()
parent.prepend(child);
adds the child element as the first child of the parent element.
7️. Removing Elements
7.1 remove()
element.remove();
removes the selected element from the DOM (web page).
7.2 removeChild()
parent.removeChild(child);
removes the specified child element from the parent element.
8. Event Handling
8.1 Inline Event (Not Recommended)
<button onclick="alert('Hello')">Click</button>
8.2 Using onclick
element.onclick = function() {
alert("Clicked");
};
8.3 addEventListener() (Best Practice)
element.addEventListener("click", function() {
alert("Clicked");
Prepared By: Kale J. N.
July 17, 2026 | Page 17 of 18
Prepared By: Kale J. N.
});
8.4 Removing Event
element.removeEventListener("click", handler);
9. Event Object
element.addEventListener("click", function(event) {
console.log(event.target);
});
Common Properties:
 event.target
 event.type
 event.preventDefault()
 event.stopPropagation()
Prepared By: Kale J. N.
July 17, 2026 | Page 18 of 18