const, let,
varconst x = 1;
let y = 2;
var z = 3;
varvar x = 1;
if (x === 1) {
var x = 2;
console.log(x);
// expected output: 2
}
console.log(x); // expected output: 2
letlet y = 1;
if (y === 1) {
let y = 2;
console.log(y);
// expected output: 2
}
console.log(y); // expected output: 1
Local variable (within the block)
constOnce assigned, can’t change values
if (condition) {
/* code to run if condition is true */
} else {
/* run some other code instead */
}

== and !=if-elseif (choice === "sunny") {
s = "Wear shorts!";
} else if (choice === "rainy") {
s = "Take umbrella";
} else {
s = "";
}
if-elseif (choice === "sunny") {
if (temp >= 70) {
s = "Wear shorts!";
} else if (temp <= 40) {
s = "Pants!";
} else {
s = "Your choice!"
}
}
&& (and)
|| (or)
! (not)
if ((choice === "sunny") && (temp >= 70)) {
s = "Wear shorts!";
}
forfor (initializer; condition; final-expression) {
// code to run
}
forfor (let i = 1; i < 10; i++) {
// code to run
}
whileinitializer
while (condition) {
// code to run
final-expression
}
whilelet N = 10;
let i = 0;
while (i < N) {
// code to run
i++;
}
do-while
initializer
do {
// code to run
final-expression
} while (condition)
do-whilelet N = 10;
let i = 0;
do {
// code to run
i++;
} while (i < N);
const single = 'Single quotes';
const double = "Double quotes";
const backtick = `Backtick`;
console.log(single);
console.log(double);
console.log(backtick);
const greeting2 = "Hello";
const name2 = "HCDD 340!";
console.log(greeting2 + ", " + name2); // "Hello, HCDD340!"
${ }
${ }const name = "HCDD 340";
const greeting = `Hello, ${name}`;
console.log(greeting);
const newline = `One day you finally knew
what you had to do, and began,`;
console.log(newline);
/*
One day you finally knew
what you had to do, and began,
*/
Line breaks are maintained
const sequence = [1, 1, 2, 3, 5, 8, 13];
const sequence = [1, 1, 2, 3, 5, 8, 13];
console.log(sequence.length);
const shopping = ["bread", "milk", "cheese",
"hummus", "noodles"];
console.log(shopping[0]);
Index starts at Zero
const shopping = ["bread", "milk", "cheese",
"hummus", "noodles"];
shopping[0] = "tahini";
console.log(shopping[0]);
push –> adds at the endunshift –> adds at the frontconst cities = ["Manchester", "Liverpool"];
cities.push("Cardiff");
console.log(cities);
// [ "Manchester", "Liverpool", "Cardiff" ]
cities.push("Bradford", "Brighton");
console.log(cities);
// [ "Manchester", "Liverpool", "Cardiff", "Bradford", "Brighton" ]
unshiftconst cities = ["Manchester", "Liverpool"];
cities.unshift("Edinburgh");
console.log(cities);
// [ "Edinburgh", "Manchester", "Liverpool" ]
unshift –> adds at the front
pop –> from the endshift –> from the frontconst cities = ["Manchester", "Liverpool"];
const removedCity = cities.pop();
console.log(removedCity); // "Liverpool"
shiftconst cities = ["Manchester", "Liverpool"];
cities.shift();
console.log(cities); // [ "Liverpool" ]
shift –> from the front
for loopconst arr = [1, 2, 3, 4];
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
for-of loopfor (let element of arr) {
console.log(element);
}
function myFunction() {
alert("hello");
}
myFunction();
// calls the function once
function addition(a, b) {
return a + b;
}
console.log(addition(1, 2));
function addition(a, b=10) {
return a + b;
}
console.log(addition(9));
console.log(addition(9, 3));
const myAddition = (a, b = 10) => {
return a + b;
}
console.log(myAddition(9));
const oneLiner = (a, b) => a + b;
console.log(oneLiner(12, 5));
const noParam = () => {
console.log("hello");
}
noParam function definitionfunction repeat(n, action) {
for (let i = 0; i < n; i++) {
action(i);
}
}
// Note console.log is a function
repeat(3, console.log);
function greaterThan(n) {
return m => m > n;
}
let greaterThan10 = greaterThan(10);
console.log(greaterThan10(11));
repeat functionlabels arraypush or unshiftlet labels = [];
// Add your code here
// repeat( ... )
console.log(labels);
// [ 0, 1, 2, 3, 4 ]
let labels = [];
// Add your code here
repeat(5, i => {
labels.push(i);
});
console.log(labels);
// [ 0, 1, 2, 3, 4 ]
const objectName = {
member1Name: member1Value,
member2Name: member2Value,
member3Name: member3Value,
};
const person = {
name: ["Bob", "Smith"],
age: 32,
bio() {
console.log(`${this.name[0]} is ${this.age} years old.`);
},
introduceSelf() {
console.log(`Hi! I'm ${this.name[0]}.`);
},
};
person.age;
person.bio();
Using dot
person["age"];
person["name"]["first"];
Using bracket
const person = {
name: ["Bob", "Smith"],
age: 32,
bio() {
console.log(`${this.name[0]} is ${this.age} years old.`);
},
introduceSelf() {
console.log(`Hi! I'm ${this.name[0]}.`);
},
};
person.name;
person.name[0];
person.age;
person.bio();
// "Bob is 32 years old."
person.introduceSelf();
// "Hi! I'm Bob."
this?this points to
current objectconst person = {
// …
introduceSelf() {
console.log(`Hi! I'm ${this.name[0]}.`);
},
};
this points to
current objectconst person1 = {
name: "Chris",
introduceSelf() {
console.log(`Hi! I'm ${this.name}.`);
},
};
const person2 = {
name: "Deepti",
introduceSelf() {
console.log(`Hi! I'm ${this.name}.`);
},
};
What will happen for person1.introduceSelf()?
person.age = 45;
person["name"]["last"] = "Cratchit";
main.js:college should be an array of objects with property
nameindex.html
from the last class activity
nodeHTML)document.querySelector returns
null if no match// first element matching a class
document.querySelector(".myclass");
// by id
document.querySelector("#log");
/* first element with
- with the name "login"
- located inside a `<div>`
- with the class `user-panel main`
*/
document.querySelector(
"div.user-panel.main input[name='login']"
);
querySelectorAll()
returns all matching nodes as an arrayconst link = document.querySelector("a");
link.textContent = "HCDD 340";
link.href = "https://hcdd-340.github.io/"
para.style.color = "white";
para.style.backgroundColor = "black";
para.style.padding = "10px";
para.style.width = "250px";
para.style.textAlign = "center";
Learn to red

document.querySelectorstyle.coloraddEventListener()const button = document.querySelector("button");
button.addEventListener("click", (event) => {
console.log("clicked!")
console.log(event);
});
const button = document.querySelector("button");
button.addEventListener("click", (event) => {
console.log("clicked!")
console.log(event);
});
const button = document.querySelector("button");
button.addEventListener("click", (event) => {
console.log("clicked!")
console.log(event);
});
What do you see in Console?