JavaScript Arrays, and DOM
JavaScript Arrays, and DOM
JavaScript Notes
JAVASCRIPT ARRAYS
Learning Objectives
• Define an array.
• Explain why arrays are used.
• Create arrays in JavaScript.
• Access and modify array elements.
• Use common array methods.
• Loop through an array.
• Apply arrays in real-life situations.
Introduction to Arrays
An array is a special variable that stores multiple values inside one variable.
Now all student names are stored inside one variable called students.
Page 2 of 14
Examples include:
• Student names
• Subjects
• Examination marks
• Courses
• Products
• Prices
• Categories
• Songs
• Albums
• Artists
• Player names
• Teams
• Scores
Index Value
0 Apple
1 Banana
2 Orange
3 Mango
Page 3 of 14
Creating Arrays
Method 1 (Recommended)
Method 2
Although both methods work, the first one is easier and more common.
[Link](fruits[0]);
Output
Apple
[Link](fruits[1]);
Output
Banana
[Link](fruits[2]);
Output
Orange
fruits[1] = "Mango";
[Link](fruits);
Output
["Apple","Mango","Orange"]
Use length.
[Link]([Link]);
Output
1. push()
[Link]("Orange");
[Link](fruits);
Output
["Apple","Banana","Orange"]
2. pop()
[Link]();
Output
["Apple","Banana"]
3. shift()
Page 5 of 14
[Link]();
Output
["Banana"]
4. unshift()
[Link]("Mango");
Output
["Mango","Banana"]
5. includes()
[Link]([Link]("Orange"));
Output
true
6. indexOf()
[Link]([Link]("Orange"));
Output
7. sort()
Sorts alphabetically.
[Link]();
[Link](fruits);
Output
["Apple","Banana","Orange"]
8. reverse()
[Link]();
Output
["Orange","Banana","Apple"]
for(let i=0;i<[Link];i++){
[Link](fruits[i]);
Output
Apple
Banana
Orange
Using for...of
let fruits=["Apple","Banana","Orange"];
[Link](fruit);
Practical Example
let students=[];
[Link]("John");
[Link]("Mary");
[Link]("James");
[Link](students);
Output
["John","Mary","James"]
Common Mistakes
Wrong
fruits[1]
Correct
fruits[0]
Wrong
let fruits=[Apple,Banana];
Correct
let fruits=["Apple","Banana"];
Summary
Arrays
Page 8 of 14
Exercise
JAVASCRIPT OBJECTS
Learning Objectives
• Define an object.
• Create objects.
• Access object properties.
• Modify object values.
• Add and delete properties.
• Create object methods.
• Apply objects in real-world situations.
What is an Object?
Property Value
Name John
Age 20
Course ICT
let student={
Page 9 of 14
name:"John",
age:20,
course:"ICT"
};
• A student
• A teacher
• A car
• A phone
• A bank account
• A product
Dot Notation
[Link]([Link]);
Output
John
Bracket Notation
[Link](student["course"]);
Output
ICT
Modifying Properties
[Link]=21;
[Link]([Link]);
Page 10 of 14
Output
21
[Link]="Male";
Deleting Properties
delete [Link];
Object Methods
let student={
name:"John",
greet:function(){
[Link]("Welcome "+[Link]);
[Link]();
Output
Welcome John
Arrays vs Objects
Array Object
Stores a list Stores related information
Uses indexes Uses property names
Example: Fruits Example: Student
Practical Example
let laptop={
brand:"HP",
Page 11 of 14
ram:"8GB",
price:55000
};
[Link]([Link]);
Output
HP
Exercise
• Name
• Age
• Course
• County
• Phone Number
The DOM is the key that allows JavaScript to enter the house and make changes.
Page 12 of 14
<h1 id="title">Welcome</h1>
[Link]("title");
[Link]("title").innerHTML="Hello Students";
By ID
[Link]("demo");
By Class
[Link]("box");
By Tag Name
[Link]("p");
Modern Method
[Link]("#demo");
[Link](".box");
Changing Text
HTML
JavaScript
[Link]("message").textContent="Good Evening";
[Link]("message").[Link]="30px";
[Link]("message").[Link]="yellow";
HTML
<p id="demo"></p>
JavaScript
function welcome(){
[Link]("demo").innerHTML="Welcome to JavaScript";
HTML
<button id="btn">Click</button>
JavaScript
[Link]("btn").addEventListener("click",function(){
alert("Button Clicked");
});
Practical Project
HTML
<button onclick="showName()">Submit</button>
Page 14 of 14
<h2 id="output"></h2>
JavaScript
function showName(){
let username=[Link]("name").value;
[Link]("output").innerHTML="Welcome "+username;
When the user types a name and clicks Submit, the webpage displays a personalized
welcome message.
Summary