Just learning the basics of Front End development (JS, HTML, CSS)
const
: for variables that are not expected to changelet
: for variables that are expected to change
- A callback function is a function that is passed as an argument to another function
const myArray = [2,4,5];
const callback = (item) => (item * 2);
myArray.map(callback); // Once the map function has iterated the array, the callback returns the value multiplied by 2
- A regular expresion
RegExp
is used to match a text with a certain pattern
const myStr = "hello world, 2023 @ more of a string";
/^[a-z ]+,[0-9 ]+@[a-z ]+$/.exec(myStr); // We matched the exact pattern of myStr
- Most used String methods
replaceAll()
: returns a string with all matches of apattern
replaced by areplacement
(in this case, thepattern
is aRegExp
):
cons myStr = "My Dog jumped on the bed. My dog is a bad Dog";
const newStr = myStr.replaceAll(/Dd{1}og)/g, "cat"); // We replace cat for all Dog/dog instances
toUpperCase()
: returns the string value converted to uppercasesubstring()
: returns a specific substring from a string within a specified rangetrim()
: removes the whitespaces around the string and returns a new string
const myStr = " Hello World! ";
myStr.trim(); // Expected output: "Hello World!"
match()
: retrieves the result of matching a string against aRegExp
const myStr = "I'm learning JavaScript";
const found = myStr.match(/[A-Z]g/); // Expected output: Array["I","J","S"]
- Most used Array methods
push()
,pop()
,shift()
,unshift()
const myArray = [1,2,3,4,5]
myArray.pop(); // Pops the element '5' out of the array
myArray.push(6); // Pushes the element '6' at the end of the array
myArray.shift(); // Removes the first element which is '1'
myArray.unshift(0); // Adds the element '0' at the beginning of the array
slice()
: returns the portion of an array fromstart
toend
const myArray = [1,2,3,4,5,6];
const myCopyArray = myArray.slice(3); // Expected output: [4.5.6]
splice()
: changes the content of an array by removing or replacing existing elements and/or adding new elements in placefindIndex()
: returns the index of the first element that satisfies the callback function given as a parameter
const myArray = [1,2,12,33];
const callback = (element) => element > 13;
myArray.findIndex(callback); // Expected output: 3
map()
: creates a new array with the outputs of calling the callback function on every element of the arrayforEach()
: executes the callback function for each element of the array
const myArray = ['a','b','c'];
myArray.forEach((elem) => console.log(elem)); // Expected output: 'a', 'b', 'c'
includes()
: determines whether an array includes the given element returningfalse
ortrue
filter()
: creates a portion of the array filtered by the given function
const myArray = ['tree', 'sun', 'dog', 'sky'];
const newArray = myArray.filter((elem) => elem.length == 3); // Expected output: Array['sun', 'dog', 'sky']
reduce()
: executes a reducer function on each element of an array
const myArray = [1,2,3,4];
const initialValue = 0;
const sumArrayValue = myArray.reduce((acc, current) => acc + current, initialValue); // Expecte output: 10