A collection of small things I learn throughout my coding journey.
pattern - /pattern/
flags - g (global) -> /pattern/g
Shortcuts:
Quantifiers:
"hello world".replace(/ /g, ""); // output - helloworld using regex
"hello world".replaceAll(" ", ""); // output - helloworld, similar to regex
"hello world".split(" ").join(""); // output - helloworld
const fruit = "cat";
for (const c of fruit) {
console.log("single character", c);
}
/* Output
single character c
single character a
single character t */
const c = ["c", "a", "t"];
c.join(""); // cat
const numArr = [1, 2, 3];
// Replace classical for loop
for (let val of numArr) {
console.log(val); // 1,2,3
}
// finding sum using reduce
[1, 2, 3].reduce((acc, currVal) => acc + currVal); // 1+2+3 = 6
The below means, this object can have any string as a key and output will be a string.
const lookUp: { [key: string]: string } = {
A: "T",
T: "A",
C: "G",
G: "C",
};