- JS is dynamic programming language used in websites to make it interactive, we use it to talk to web browser, hence it’s called "programming language of the web".
- Created by Brendon Eich in 1995 (10 days).
- Used to interact and modify HTML.
- Where do we use JS?
- Browsers
- Servers: Node.js
- Imbedded devices
it’s a Javascript built in object that repersent the whole web page.
-
Title of the page:
document.title
-
The whole HTML body :
document.body
-
Body elements in HTML (ex: header & divs) :
document.body.children
-
Get specific element using id:
document.getElementById("board")
-
The same as previous but this to CSS sector:
document.querySelector("#board")
return the same result as above -
Get all the elements with the same tag:
document.getElementsByTagName("h1")
-
In SCC sector:
document.querySelectorAll("h1")
-
Get element with same class :
document.getElementsByClassName("player")
-
CSS sector :
document.querySelectorAll(".player")
-
Finding the nubmer of elements:
document.getElementsByClassName("player").length
or in CSSdocument.querySelectorAll(".player").length
-
Get the text element inside the selected element or container
document.getElementById("p1-name").
<body>
<header>
<h1>Tic Tac Toe</h1>
<h2>A game you know</h2>
<div id="players">
<p id="p1" class="player">Player <span id="p1-symbol">X</span>: <span id="p1-name">Anjana</span></p>
<p id="p2" class="player">Player <span id="p2-symbol">O</span>: <span id="p2-name">Marc</span></p>
</div>
</header>
<div id="board">
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
</div>
</body>
I can use the equal operator =
to assign a value to any element. Tow ways:
- .textContent = “”
- .appned(””)
document.title = "My Page"
// change the title
document.getElementById("p1-name").textContent = "Sofia"
//replace the text of the #p1-name element
document.getElementById("p1-name").append(" & friends")
// add to the end of the element's current text
document.getElementsByTagName("h2")[0].textContent = "Hello"
// return an array
document.querySelector("header h2").textContent = "sarah"
// the same as previous
document.querySelector("h1").append(" !!!")
document.querySelector("header h1").textContent="Sarah"
Are chunks of information we want to work with, that information (data) can be of different types:
- Primitive types
- string : (ex: “Sarah”, ‘Sarah’ ,
Sarah
, "20") - number : (ex: 9, 525600, 3.14. 1.2e9, Infinity)
- boolean: (ex: true, false)
- undefined: there was supposed to be sth. but accidentaly there is nothing
- null: I ment to be nothing here.
-
Objects
(e.g.
document
& friends)
👉 typeof: operator returns a string indicating the type of the operand's value. ###Exampls:
typeof("21") // string
typeof(false) // boolean
typeof(null) // object
typeof("some string".length) // number
Sequence of characters are in a specific order, each gets a number, starting at 0
"ALOHA"[0] // “A”
"ALOHA".length // 5
"ALOHA".indexOf("L") //1
// What's the index of the first accourance of specific character
// if not exist will return -1
"ALOHA".includes("HA") // true
// "ALOHA".includes("HA")
"ALOHA".startsWith("AL") // true
// Does this string start with some other string?
"ALOHA".indexOf("HA") // 3
// At what index does this substring begin?
"ALOHA" + "!"
// Connecting strings together
"ALOHA".toLowerCase() // "aloha"
// Exercises
// 1. Add your last name in the players listing
document.getElementById("p1-name").append(" abu irmeileh")
// 2. Retrieve the first "T" in the page title
document.title.indexOf("T")
// 3. Answer whether the page title contains the string "JavaScript"
document.title.includes("JavaScript")
// 4. Capitalize the heading "Tic Tac Toe"
document.querySelector("header h1").textContent = // to appear in the page
document.querySelector("h1").textContent.toUpperCase()
- developer.mozilla.org : very helpful in programming
- When reloading the page evry changes in browser console won’t be saved
- null is primitive data type, but when we write typeof null it’s an object !!
- if I declare an array using const key word I still can change it's element by index
- To ensure your data doesn't change, JavaScript provides a function Object.freeze to prevent data mutation.
- We can not change string by index
- Compare Scopes of the var and let Keywords My solution:
function checkScope() {
let i = 'function scope';
if (false) {
i = 'block scope';
console.log('Block scope i is: ', i);
}
console.log('Function scope i is: ', i);
return i;
}
- Mutate an Array Declared with const My solution:
const s = [5, 7, 2];
function editInPlace() {
// Only change code below this line
s[0]=2
s[1]=5
s[2]=7
// Using s = [2, 5, 7] would be invalid
// Only change code above this line
}
editInPlace();
- Prevent Object Mutation My solution:
function freezeObj() {
const MATH_CONSTANTS = {
PI: 3.14
};
// Only change code below this line
Object.freeze(MATH_CONSTANTS)
// Only change code above this line
try {
MATH_CONSTANTS.PI = 99;
} catch(ex) {
console.log(ex);
}
return MATH_CONSTANTS.PI;
}
const PI = freezeObj()
const magic = ()=> {
return new Date();
};
- Write Arrow Functions with Parameters My solution:
const myConcat = (arr1, arr2) =>{
return arr1.concat(arr2);
};
console.log(myConcat([1, 2], [3, 4, 5]));
- Set Default Parameters for Your Functions My solution:
// Only change code below this line
const increment = (number, value=1) => number + value;
// Only change code above this line
- Up and down, the string grows My solution:
const STRANGE_STRING = "Aß"
- Array.diff My solution:
function arrayDiff(a, b) {
return a.filter(e=>{
return !b.some(ee=>{return ee ===e})
})
}
- Beginner Series #3 Sum of Numbers My solution:
function getSum(a, b)
{
let aa = Math.min(a,b)
let bb = Math.max(a,b)
console.log(aa,bb)
let ans =0
for(let i =aa; i<=bb; i++)
ans+=i
return ans
}