Today was a productive day of revision and practice. I refreshed my understanding of previous topics, completing exercises and working on a project that applied those lessons. In addition, I explored new topics like functions and scope, specifically the differences between "let," "const," and "var" in terms of scope. It was an enlightening experience that expanded my knowledge of JavaScript
- Comments is important in JS, it Help others (and yourself)understand your code.
- Functions is used to do actions it has a parameters and it returns a value .
- Arrow function is unnamed function without much code.
- Scope determines where variables are "in play".
- There are kinds of scope like global scope it is the widest scope.
- There is a difference between "let" and "var" in their scope
// Example 1: declare the following functions :
//1. multiply: given 2 numbers, return their product
function product(n1,n2)
{ return n1*n2;
}
//2.longerThan: given 2 arrays, return whether the first is
longer than the second
function longerThan(a1, a2) {
return a1.length>a2.length;
}
//Example 2 :declare function using arrow functions:
//divide: given 2 numbers, return the first divided by the second
const divide=(n1,n2)=>n1/n2;
//Example 3: declare and object called fact with statement
property and answer and explanation
const fact = {
statement:"Arrays like objects",
answer:true,
explanation: "Arrays are kind of objects with special properties "
};
Return a Value from a Function with Return
My Solution :
function timesFive (num){
return num*5;
}
My Solution :
let myGlobal =10;
let oopsGlobal;
function fun1( ) {
oopsGlobal =5;
}
function fun2() {
let output = "";
if (typeof myGlobal != "undefined") {
output += "myGlobal: " + myGlobal;
}
if (typeof oopsGlobal != "undefined") {
output += " oopsGlobal: " + oopsGlobal;
}
console.log(output);
}
My Solution :
function myLocalScope() {
let myVar;
console.log('inside myLocalScope', myVar);
}
myLocalScope();
console.log('outside myLocalScope', myVar);
Global vs. Local Scope in Functions
My Solution :
const outerWear = "T-Shirt";
function myOutfit() {
const outerWear="sweater";
return outerWear;
}
myOutfit();
My Solution :
function nextInLine(arr, item) {
arr.push(item);
const removed = arr.shift();
return removed;
}