Basics Flashcards
Arrow functions
let addTwoNums = (n1,n2) => n1+n2;
forEach in array
let nums = [10, 20, 30]; nums.forEach((number, index, array) => { console.log(number) }); Note: number is an element of array, index and array are optional.
nums.forEach(function(number) {
console.log(number);
});
For - of: iterate over an array
for (const myElement of myArray) {
console.log(myElement);
}
Add an element to an array
PUSH(): add an element to at the END of an array
UNSHIFT(): adds it at the BEGINNING of an array
Removal elements from arrays
POP(): remove the last element of an array
SPLICE(index from which to begin removing, number of elements to remove)
Sort()
const numbers =[3, 23,12] let sorted = numbers.sort((a,b) => a-b); // 3,12,23
let sorted_desc = numbers.sort((a, b) => b-a); // 23, 12, 3
filter()
Returns a new array
let cities = [
{ name: “Los Angeles”, population: 3700000 },
{ name: “New York”, population: 8175133 },
{ name: “Chicago”, population: 2695598 },
{ name: “Houston”, population: 2099451 },
{ name: “Philadelphia”, population: 1526006 },
];
// FILTER() returns a new array with all elements that pass the test implemented by the callback() function // eg: arrayObject.filter(callback, contextObject); // another way using filter() let bigCities = cities.filter((city) => city.population > 3000000); console.log(bigCities1);
Reduce()
// acc: accumulator; c: current value; 0: starting value of accumulator
let numArr = [11, 35, 67, 14, 78, 10];
let sum = numArr.reduce((acc, c) => acc + c, 0);
console.log(sum);
In JavaScript, a function is a value, so we can deal with it as a value.
function sayHi() { alert( "Hello" ); }
alert( sayHi ); // shows the function code
Please note that the last line does not run the function, because there are no parentheses after sayHi. There are programming languages where any mention of a function name causes its execution, but JavaScript is not like that.
function sayHi() { // (1) create alert( "Hello" ); }
let func = sayHi; // (2) copy
func(); // Hello // (3) run the copy (it works)! sayHi(); // Hello // this still works too (why wouldn't it)
The Function Declaration (1) creates the function and puts it into the variable named sayHi.
Line (2) copies it into the variable func. Please note again: there are no parentheses after sayHi. If there were, then func = sayHi() would write the result of the call sayHi() into func, not the function sayHi itself.
Now the function can be called as both sayHi() and func().
Array constructor with multiple parameters
let fruits = new Arrat(‘apple’, ‘banana’);
console. log(fruits.length); //2
console. log(fruits[0]); //‘apple’
Example 2:
let colors = new Array(2);
console. log(colors.length); //2
console. log(colors[0]); //undefined
Linking to Same Page in HTML
In order to link to a target on the same page, we must give the target an ID.
EXAMPLE:
<p>This is the top of the page</p>
<p></p>
<h1>This is the bottom!</h1>
The target link is a string containing the octothorpe # character and the target element’s id.
<ol>
<li><a>Top</a></li>
<li><a>Bottom</a> </li></ol>
Linking to relative page.
Files are store on the same folder-> can use a relative path
Eg: ./index.html
<a>Contact</a>