JavaScript Functions
JavaScript Functions
JavaScript Functions
turn array into set, let arrayAnimals = ['lion', 'horse', 'snake', 'mouse'];
const animals = new Set(arrayanimals);
Weak set, only use object data types, use same method types like add and delete
let nums = [1, 2, 3, 4]; let weakSet = new WeakSet(nums); console.log(weakSet);
// {Array(4)}, no value output
Maps(perform better involving adding and removing key value pairs, as object is not
optimal), allow keys of any types, object covert keys to strings as map will not
-let gameMap = new Map(); .set to add value pairs, gameMap.set('Name',
'Mario').set(2, 'number two').set(true, 'boolean');
-returns the relevant value .get, let getInfo = gameMap.get('Name');
console.log(getInfo); // Mario
-returns boolean whether element exists .has, let hasInfo = gameMap.has('Name');
console.log(hasInfo); // true
-delete info inside map, let deleteInfo = gameMap.delete(true);
console.log(deleteInfo); // 'Name' => 'Mario', 2 => 'number two' true,
'boolean is deleted
-check how many values, gameMap.size // Map(2) {'Name' => 'Mario', 2 =>
'number two'}
-3 methods to iterate though maps, keys(), values(), entries()
keys() loop over the key/name in key value pairs, for (let key of gameMap.keys())
{ console.log(key); } // Name 2 true
values() loop over the value in key value pairs, for (let value of
gameMap.values()) { console.log(value); } // Mario number two boolean
entries() loop over both key and value in key value pairs, for (let [key, value]
of gameMap.entries() { console.log(`${key} has the value of ${value}); }
// Name has the value of Mario 2 has the value of number two...
Weak map, use object data, the keys most be objects, do not support iteration,
rarely used
let weakMap = new WeakMap(); output error when using non-object values
FUNCTION, can be treated as objects, divide our code into reusable parts, all the
code between the curly braces will be executed every time the function is called
invoke/excute functionName(); can add variables inside the function
name() { let title = 'Valorant'; }
4 ways to create function, 1. function declaration and expression(most common) 2.
arrow functions 3. function constructors(rarely used) 4. generator functions
function name is mandatory
-Parameters, variables that act as placeholders for the values that are to be input
to a function when called
Parameters
-function declaration, function area(width, height) {
console.log(width * height);
} Arguments, actual values that are inputed/passed into a function when called
area(5, 3); // 15
Function declaration can be invoked earlier than they are created, unlike
expression, Javascript store in memory any global scope that is why it have
access it anywhere myFunct(); // Hi myFunct(); // error,
because function expression is created once we get to the line it's on
function myFunct() { console.log('Hi'); } let myFunct = function()
{ console.log('Hi'); }
-Default parameters, allow parameter to have a predetermined value in case no
argument is passed into the function or the argument is undefined,
is always at the end of the parameter list, in order to counteract any missing
arguments that turn into undefined
function favFood (response = "empty, please add your answer") {
console.log(`Your favourite food is ${response}`); }
favFood(); // Your favourite food is empty, please add your answer
favFood('pizza') // Your favourite food is pizza
-Default Error
function favGame (game) { if (game === undefined) { console.log('Please enter your
fav game!');
} else { console.log(`Your fav game is ${game}`); } } favGame(); // Please
provide yout fav game!
-Return, evaluates and ends the execution of the function immediately, specifies a
value to be returned to the function caller,
return statement send a value back out of a function, can have only 1 return per
code block { }
most commonly assign the function call to another variable
monitorCount takes argument for rows and columns and returns a value equal
to rows * columns
function monitorCount(rows, columns) {
return rows * columns; }
*const numOfMonitors = monitorCount(4, 5);
console.log(numOfMonitors); // 20 need to assign a new variable if return is
present inside the function
-Helper Function, return value of a function can be used inside another function
function multiplyByNineFifths(number) {
return number * (9/5);
};
function getFahrenheit(celsius) {
return multiplyByNineFifths(celsius) + 32;
};
const temperture = getFahrenheit(15); longer code
console.log(temperture) // 59
-Arrow function
const plantNeedsWater = (day) => {
if (day === "Monday") {
return true;
} else {
return false; } };
console.log(plantNeedsWater("Wednesday")); // Wednesday shorter code
all comparison operators ===, <, >, <=, >= returns boolean true or false value
function isEqual(a, b) { return a === b; } console.log(isEqual(10, 15)) //
false
-const canIVote = age => { const canIVote = (age) => age >= 18 ? true :
false
if (age >= 18) { console.log(canIVote(19))
return true;
} else {
return false;
}
};
function caseInSwitch(val) {
let answer = "";
switch (val) {
case 1:
answer = "alpha";
break;
case 2:
answer = "beta";
break;
case 3:
answer = "gamma";
break;
case 4:
answer = "delta";
break;
}
return answer;
}; console.log(caseInSwitch(4)); // output delta
When using === else if is faster, but switch is more readable when making more than
3 comparisons
-Switch, else if, case values are tested with strict equality(===)
if (val == 1) { switch (val) { case is the value in if/else if
statements
answer = "a"; case 1: case 2:
} else if (val == 2) { answer = 'a'; answer = 'b';
answer = "b"; break; break;
} else { default:
answer = ""; answer = ''; }
}