Lecture 3 JS Part B
Lecture 3 JS Part B
What is String?
If you what to include special characters into string then use The backslash (\) escape character
\\ \ Backslash
let text = "We are the so-called \"Vikings\" from the north.";
Other list is
\b Backspace
\f Form Feed
\n New Line
\r Carriage Return
\t Horizontal Tabulator
\v Vertical Tabulator
String comparison
String Methods
OR
OR
OR
OR
Uses back-tick `` instead of quotes for a string and they are used for variable manipulations of a
string
OR
Array
An array is a special variable, which can hold more than one value.
Syntax:
OR
NB: Do not use new Array approach if your element during declare and
initialization is one, will results into undefined
A Common Error
const points = [40];
Changing array
Array object or simply object (Array as object , in Python are called Dictionary)
Instead of using index to manipulated array items, they use key. The key or ID should be a string
pattern.
Array object uses {} while array uses [] and map uses ()
Array Elements
Sort
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
OR for numbers
OR
const points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b - a});
function myFunction(value) {
txt += value + "<br>";
}
map() const numbers1 = [45, 4, 9, 16, 25];
const numbers2 = numbers1.map(myFunction);
To use keyscollection
Let text;
for (let x of keyscollection) {
text += x + "<br>";
}
then assign text to ularray.children[1].innerHTML or
ularray.children[1].textContent or
ularray.children[1].innerText
fruits.includes("Mango"); // is true
An array declared with const has Block Scope.An array declared in a block is not the same as an array
declared outside the block.
Map
A Map holds key-value pairs where the keys can be any datatype while in object only string
A Map remembers the original insertion order of the keys.\
Create a Map
["bananas", 300],
["oranges", 200]
])
// Create a Map
const fruits = new Map();
fruits.set("apples", 200);
fruits.get("apples");
fruits.delete("apples");
fruits.has("apples");
Fruits.size()
Map Method
Events
HTML events are "things" that happen to HTML elements. Here are some examples of HTML
events:
- An HTML web page has finished loading
- An HTML input field was changed
- An HTML button was clicked
Structure:
1. Writing in HTML
2. Writing in Javascript
button.addEventListener("click", myFunction);
Example
<button onclick="document.getElementById('demo').innerHTML = Date()">The
time is?</button>
JavaScript code is often several lines long. It is more common to see event
attributes calling functions:
Example
<button onclick="displayDate()">The time is?</button>
Event Description
onmouseout The user moves the mouse away from an HTML element
2. Witting in JS.
button.addEventListener("click", myFunction);
Class
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
}
class Methods
class ClassName {
constructor() { ... }
method_1() { ... }
method_2() { ... }
Ardhi UniversityLecture_3_part_b 2023
method_3() { ... }
}
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
age() {
const date = new Date();
return date.getFullYear() - this.year;
}
}
Class Inheritance
class Car {
constructor(brand) {
this.carname = brand;
}
present() {
return 'I have a ' + this.carname;
}
}
class Car {
constructor(brand) {
this.carname = brand;
}
get cnam() {
return this.carname;
}
set cnam(x) {
this.carname = x;
}
}
document.getElementById("demo").innerHTML = myCar.cnam;
class Car {
constructor(name) {
this.name = name;
}
static hello() {
return "Hello!!";
}
}
JS DOM
Method Description
Property Description
Method Description
document.getElementById(id).onclick = function(){code}