Javascript
Javascript
Javascript
let variable can change by, let favColor = "Red"; favColor = "Green";
console.log(favColor) // Green
Template Literals
let name = 'Liu'
let birthday = 7252002
without, let info = name + ' was born on this date ' + birthday + ' .'
with (much easier), let info2 = `${name} was born on this date ${birthday}`.
The HTML parser does NOT process the next element in the HTML file until it loads
and executes the <script> element, thus
leading to a delay in load time and resulting in a poor user experience.
Additionally, scripts are loaded sequentially, so if one script depends on another
script, they should be placed in that
very order inside the HTML file.
If statements are used to make decisions in code, tells JS to execute code under
certain conditions(boolean conditions)
>, >=, <, <= 6 < 5 // false '6' >= 5 // true
Equality operator(==) 3 == 3 // true 3 == '3' // true typeof
determine the type of a variable or value
Strict equality(===) 3 === 3 // true 3 === '3' // false type
3(number) type '3'(string)
function compareEquality(a, b) {
if (a === b) {
return "Equal";
}
return "Not Equal";
}
compareEquality(10, "10"); // false
Inequality operator(!=), returns false where equality would return true and vice
versa
1 != "1" // false 1 != 2 // true
Strict inquality operator, returns false where strict equality would return true
and vice versa
2 !== "2" // true 2 !== 2 // false
-Logical and operator(&&), returns true if and only if the operands to the left and
right of it are true
const randomNum = num => {
if (num > 5 && num < 10) {
return "Yes";
} else { return "No"; }
}; console.log(randomNum(1)) // "No"
-IF ELSE, when a condition is false an alternate block of code can be executed
function testElse(val) {
let result = "";
if (val > 5) {
result = "Bigger than 5";
} else {
result = "5 or Smaller";
} return result;
};
console.log(testElse(5)) // output 5 or Smaller
if has (), if (val > 5) else does not have ()
let age = 18
let message = age >= 18 ? 'can vote' : 'Sorry, can not vote'; // ouput can vote
condition ? [true] : [false]
-ELSE IF, more than 2 possible outcomes, multiple conditions that need to be
addressed
let stopLight = "yellow"; NOT A FUNCTION
if (stopLight === "red") {
console.log("Stop");
} else if (stopLight === "yellow") {
console.log("Slow down");
} else if (stopLight === "green") {
console.log("Go");
} else { console.log("Caution, unknown");
}; stopLight // output Slow down 'yellow'
else if has (), else if (val < 5)
Scope is where a variable is declared and from where it can be accessed, vaiable
can have same name if declared in different code blocks
-Local Scope, only visble inside the function that they are created in, restricted
to being usable only inside that function, and are more secured
function game() { let title = 'Valorant'; console.log(title); } game(); //
Valorant if (true) { let title = 'Valorant'; console.log(title); }
// Valorant can't be use outside the function, only defined inside the funtion
not global console.log(title); // error
-Global Scope, variables declared outside the function block or anywhere else
let company = 'Riot Games'; function game() { let title = 'Valorant';
console.log(title, company); } game(); // Valorant Riot Games
can be use anywhere console.log(company); // Riot Games
Local Scope takes precedence over the global scope
let processed = 0;
function processArg(num) {
return (num + 3) / 5; };
processed = processArg(7); console.log(processed) // output 2
-Function hoisting, broswer scan through and found the function declaration and
pulled it to the top, ready to use
Declare, Invoke function lion('Aslan'); function lion(name)
{ console.log(`The lion's name is ${name}`); } // The lion's name is Aslan
Init, Invoke function lion('Aslan'); let lion = function(name)
{ console.log(`The lion's name is ${name}`); } // Error