Hope you did well on your exam!

If this site helped, please leave feedback, check out the project on GitHub, or follow me on GitHub.

F6: JavaScript 2 & AJAX code examples

Code examples from the course Google Drive for the JavaScript 2 & AJAX lecture. View the source and run live previews where available.

Back
ES5 and ES6 Demo โ€” string trim

11.html

HTML17 lines
<!DOCTYPE html>
<html>
<head>
<script>
var e = " Hello World ";
alert("Before trim: '" + e + "'\nAfter trim: '" + e.trim() + "'");
</script>
</head>
<body>
<h2>ES5 and ES6 Demo</h2>
</body>
</html>

ES5 and ES6 Demo โ€” Array.isArray

12.html

HTML18 lines
<!DOCTYPE html>
<html>
<head>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
//var fruits =5;
document.writeln(Array.isArray(fruits));
</script>
</head>
<body>
<h2>ES5 and ES6 Demo</h2>
</body>
</html>

ES5 and ES6 Demo โ€” forEach

13.html

HTML25 lines
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction(value)
{
txt = txt+ value;
//console.log(txt);
document.write(txt);
}
var txt ="";
var numbers = [45,4,9,16,25];
numbers.forEach(myFunction);
</script>
</head>
<body>
<h2>ES5 and ES6 Demo</h2>
</body>
</html>

ES5 and ES6 Demo โ€” map

14.html

HTML24 lines
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction(value)
{return value *2;}
var numbers1 = [45, 4, 9, 16, 25];
var numbers2 = numbers1.map(myFunction);
document.writeln(numbers1);
document.writeln("<br>");
document.writeln(numbers2);
</script>
</head>
<body>
<h2>ES5 and ES6 Demo</h2>
</body>
</html>

ES5 and ES6 Demo โ€” filter

15.html

HTML22 lines
<!DOCTYPE html>
<html>
<head>
<script>
var numbers = [45,4,9,16,25];
var over18 = numbers.filter(myFunction);
function myFunction(value)
{return value >18;}
document.writeln(over18)
</script>
</head>
<body>
<h2>ES5 and ES6 Demo</h2>
</body>
</html>

ES5 and ES6 Demo โ€” reduceRight

16.html

HTML22 lines
<!DOCTYPE html>
<html>
<head>
<script>
var numbers1 = [45,4,9,16,25];
var sum = numbers1.reduceRight(myFunction);
function myFunction(total, value,index, array)
{return total - value;}
document.writeln(sum)
</script>
</head>
<body>
<h2>ES5 and ES6 Demo</h2>
</body>
</html>

ES5 and ES6 Demo โ€” every and some

17.html

HTML25 lines
<!DOCTYPE html>
<html>
<head>
<script>
var numbers = [45,4,9,16,25];
//var allOver18 = numbers.every(myFunction);
var someOver18 = numbers.some(myFunction);
function myFunction(value, index, array)
{return value > 18; }
//document.writeln(allOver18);
document.writeln(someOver18)
</script>
</head>
<body>
<h2>ES5 and ES6 Demo</h2>
</body>
</html>

ES5 and ES6 Demo โ€” find

18.html

HTML22 lines
<!DOCTYPE html>
<html>
<head>
<script>
var numbers = [4,9,16,25,29];
var first = numbers.find(myFunction);
function myFunction(value, index, array) {
return value > 18;
}
document.writeln(first);
</script>
</head>
<body>
<h2>ES5 and ES6 Demo</h2>
</body>
</html>

ES5 and ES6 Demo โ€” JSON.stringify

19.html

HTML19 lines
<!DOCTYPE html>
<html>
<head>
<script>
var JSONobj = {name:"John", age:30, city:"New York"};
var myJSONstring = JSON.stringify(JSONobj);
document.writeln(myJSONstring);
</script>
</head>
<body>
<h2>ES5 and ES6 Demo</h2>
</body>
</html>

ES5 and ES6 Demo โ€” filter with arrow functions

20.html

HTML33 lines
<!DOCTYPE html>
<html>
<head>
<script>
//ES5
var numbers1 = [45,4,9,16,25];
var over18_es5 = numbers1.filter(myFunction);
function myFunction(value)
{
return value > 18;
}
document.writeln("over18_es5: " + over18_es5 + "<br>");
//ES6
var numbers2 = [45,4,9,16,25];
var over18_es6 = numbers2.filter((value)=>value>18);
document.writeln("over18_es6: " + over18_es6 + "<br>");
</script>
</head>
<body>
<h2>ES5 and ES6 Demo</h2>
</body>
</html>

ES5 and ES6 Demo โ€” flatMap

22.html

HTML31 lines
<!DOCTYPE html>
<html>
<head>
<script>
/* function sum(...args){
let sum = 0;
for (let arg of args) sum += arg;
return sum;}
let x = sum(4,9,16,25,29,100,66,77);*/
let numbers=[1,2,3,4];
let result = numbers.flatMap(x => [x, x * 2]);
console.log(result);
document.writeln(result);
</script>
</head>
<body>
<h2>ES5 and ES6 Demo</h2>
<!--<div id="demo"><script>document.writeln(x)</script></div>-->
</body>
</html>

ES5 and ES6 Demo โ€” String.padStart

23.html

HTML33 lines
<!DOCTYPE html>
<html>
<head>
<script>
//pad the String with 0
let product_cost = '1699'.padStart(7,0);
document.writeln(product_cost);
document.writeln("<br>");
document.writeln(product_cost.length);
document.writeln("<br>");
//pad the String with blank spaces
let product_cost1 = '1699'.padStart(7);
document.writeln(product_cost1);
document.writeln("<br>");
document.writeln(product_cost1.length);
document.writeln("<br>");
</script>
</head>
<body>
<h2>ES5 and ES6 Demo</h2>
<div id="demo"></div>
</body>
</html>

Promise pizza example โ€” basic Promise

200.html

HTML47 lines
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>Promise Pizza Example</h2>
<div id="output"></div>
<script>
console.log("1. You ordered a pizza.");
const output = document.getElementById("output");
function log(message) {
console.log(message);
output.innerHTML += message + "<br>";
}
log("1. You ordered a pizza.");
const pizzaOrder = new Promise((resolve, reject) => {
// Simulate the pizza shop preparing the order (3 seconds)
setTimeout(() => {
let pizzaReady = false; // Change to false to simulate rejection
if (pizzaReady) {
resolve("3. Pizza is here! ");
} else {
reject("3. Sorry, no pizza today. ");
}
}, 3000); // 3-second delay simulates the "pending" state
});
log("2. While waiting, you watch a movie. ");
pizzaOrder
.then((message) => {
log(message);
})
.catch((error) => {
log(error);
});
</script>
</body>
</html>

ES5 and ES6 Demo โ€” Promise producer and consumer

21.html

HTML32 lines
<!DOCTYPE html>
<html>
<head>
<script>
let myPromise = new Promise(function(myResolve, myReject) {
//producing code-- main function that is taking time
setTimeout(function()
{
myResolve("I love You !!");
},3000);});
//consuming code-- function that will run after the main function
myPromise.then(function(value) {
document.getElementById("demo").innerHTML= value;
});
</script>
</head>
<body>
<h2>ES5 and ES6 Demo</h2>
<div id="demo"></div>
</body>
</html>

Async/await pizza example

201.html

HTML54 lines
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>Async/await pizza example</h2>
<div id="output"></div>
<script>
function log(message) {
console.log(message);
document.getElementById("output").innerHTML += message + "<br>";
}
function orderPizza() {
return new Promise((resolve, reject) => {
setTimeout(() => {
let pizzaReady = true;
if (pizzaReady) {
resolve("Pizza is ready!");
} else {
reject(" Pizza shop ran out of dough.");
}
}, 5000);
});
}
async function dinnerTime() {
log("1. You ordered a pizza.");
let seconds = 0;
const movieInterval = setInterval(() => {
seconds++;
log("Watching movie... " + seconds + " seconds passed");
}, 1000);
try {
const pizza = await orderPizza();
log("3. " + pizza);
} catch (error) {
log("3. " + error);
}
clearInterval(movieInterval);
log("4. Now you're ready to eat! ");
}
dinnerTime();
</script>
</body>
</html>