Professional Documents
Culture Documents
Javascript object
Everything in JavaScript takes the form of an
object.
This means that it is a "package", which
contains various properties (pieces of
information) and methods (lists of instructions
that it can carry out).
DOM
• DOM - Document Object Model
• When a web page is loaded, the browser creates a
Document Object Model of the page.
• The W3C Document Object Model (DOM) is a
platform and language-neutral interface that allows
programs and scripts to dynamically access and
update the content, structure, and style of a
document.
• The HTML DOM model is constructed as a tree of
Objects:
• The Document Object Model (DOM) defines the logical
structure of documents and the way a document is accessed
and manipulated.
The HTML DOM is a standard object model and programming
interface for HTML.
It defines:
•The HTML elements as objects
•The properties of all HTML elements
•The methods to access all HTML elements
•The events for all HTML elements
The DOM Programming Interface
<!DOCTYPE html>
<head>
<title>JavaScript Get the Length of an Array</title>
</head>
<body>
<script>
var fruits = ["Apple", "Banana", "Mango", "Orange", "Papaya"];
document.write(fruits.length); // 0utputs: 5
</script>
</body>
</html>
Looping Through Array Elements
<script>
var fruits = ["Apple", "Banana", "Mango", "Orange", "Papaya"];
for(var i = 0; i < fruits.length; i++)
{
document.write(fruits[i] + "<br>"); // Print array element
}
</script>
var fruits = ["Apple", "Banana", "Mango", "Orange", "Papaya"];
// Iterates over array elements
for(var fruit of fruits)
{
document.write(fruit + "<br>");
}
var fruits = ["Apple", "Banana", "Mango", "Orange", "Papaya"];
for(var i in fruits)
{
document.write(fruits[i] + "<br>");
}
Adding New Elements to an Array
<!DOCTYPE html>
<html lang="en"> To add a new element at the end of
<head> an array, use the push() method
</head> to add a new element at the
<body> beginning of an array use
<script> the unshift() method,
var colors = ["Red", "Green", "Blue"];
colors.push("Pink", "Voilet");
colors.unshift("Yellow", "Grey");
document.write(colors + "<br>");
document.write(colors.length);
</script> OUTPUT
</body>
</html> Yellow,Grey,Red,Green,Blue,Pink,Voilet
7
Removing Elements from an Array
To remove the last element from an array use the pop() method.
<html >
<head>
<title>JavaScript Remove the Last Element from an Array</title>
</head>
<body>
<script>
var colors = ["Red", "Green", "Blue"];
var last = colors.pop();
document.write(last + "<br>"); // Prints: Blue
document.write(colors.length); // Prints: 2
</script>
</body>
</html>
remove the first element from an array using the shift()
method
<html >
<head>
<title>JavaScript Remove the First Element from an
Array</title>
</head>
<body>
<script>
var colors = ["Red", "Green", "Blue"];
var first = colors.shift();
<script>
var colors = ["Red", "Green", "Blue"];
document.write(colors.toString()); // Prints: Red,Green,Blue
</script>
Splicing and Slicing Arrays
Adding or Removing Elements at Any Position
<script>
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
document.getElementById("demo1").innerHTML = fruits;
var citrus = fruits.slice(1,3); //Banana,Orange,Lemon,Apple,Mango
document.getElementById("demo2").innerHTML = citrus; //Orange,Lemon
</script>
<script>
let languages = ["JavaScript", "Python", "C", "C++", "Java"];
let one = languages.slice();
document.getElementById("demo1").innerHTML = one;
let two = languages.slice(1, 4);
document.getElementById("demo2").innerHTML = two;
</script>
Sorting and reversing an array
The sort() method sorts an array alphabetically
The reverse() method reverses the elements in an array.
<script>
document.getElementById("demo1").innerHTML = fruits.sort();
document.getElementById("demo2").innerHTML =
fruits.reverse();
</script>
Searching Through an Array
document.write(fruits.indexOf("Apple") + "<br>"); //
Prints: 0
document.write(fruits.indexOf("Banana") + "<br>");
// Prints: 1
document.write(fruits.indexOf("Pineapple"));
// Prints: -1
</script>
JavaScript Strings
JavaScript strings are for storing and manipulating text.
<script>
let one = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.getElementById("demo").innerHTML = one.length;
26
knowledge is "power"
JavaScript String Methods
<p id="demo"></p>
<script>
let str = "Apple, Banana, Kiwi";
document.getElementById("demo").innerHTML =
str.slice(-12,-6);
</script>
<p id="demo"></p>
<script>
let str = "Apple, Banana, Kiwi";
document.getElementById("demo").innerHTML =
str.substr(7,3); //Ban
</script>
<button onclick="myFunction2()">lower</button>
<p id="demo2">HELLO!</p>
<script>
function myFunction1() {
let text1 = document.getElementById("demo1").innerHTML;
document.getElementById("demo1").innerHTML =
text1.toUpperCase();
}
</script>
<script>
function myFunction2() {
let text2 = document.getElementById("demo2").innerHTML;
document.getElementById("demo2").innerHTML =
text2.toLowerCase();
}
JavaScript Search Methods
String indexOf()
indexOf() method returns the index of (the position of)
the first occurrence of a specified text in a string.
indexOf(), and lastIndexOf() return -1 if the text is not found
String lastIndexOf()
The lastIndexOf() methods searches backwards (from the end to
the beginning)
String startsWith()
String endsWith()
0
<p id="demo1"></p> 32
<p id="demo2"></p>
<p id="demo3"></p> 32
<p id="demo4"></p> 0
<p id="demo5"></p> 8
<script>
let str1 = "vellore institute of technology vellore";
document.getElementById("demo1").innerHTML = str1.indexOf("vellore");
let str2 = "vellore institute of technology vellore";
document.getElementById("demo2").innerHTML = str2.lastIndexOf("vellore");
let str3 = "vellore institute of technology vellore";
document.getElementById("demo3").innerHTML = str3.indexOf("vellore",20);
let str4 = "vellore institute of technology vellore";
document.getElementById("demo4").innerHTML = str4.lastIndexOf("vellore",
25);
let str5 = "vellore institute of technology vellore";
document.getElementById("demo5").innerHTML = str5.search("ins");
</script>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
<script>
let text1 = "knowledge is power";
document.getElementById("demo1").innerHTML =
text1.startsWith("knowledge");
let text2 = "knowledge is power";
document.getElementById("demo2").innerHTML = text2.startsWith("vit");
let text3 = "knowledge is power";
document.getElementById("demo3").innerHTML =
text3.endsWith("power");
</script>