Skip to content

Instantly share code, notes, and snippets.

@gabrielheinrich
Created November 16, 2022 13:42
Show Gist options
  • Save gabrielheinrich/3d8577ce556db7e282df3a0fa073b666 to your computer and use it in GitHub Desktop.
Save gabrielheinrich/3d8577ce556db7e282df3a0fa073b666 to your computer and use it in GitHub Desktop.
// 1. Try to name all eight Javascript types
// number
const num = 1;
// string
const str = "Hello";
// boolean
const bool = true; // or false
// null
const n = null;
// undefined
const u = undefined;
// bigint
const bigInt = BigInt("10000000000000000000000000");
// symbol
const sym = Symbol("Hello");
// object
const obj = {
a: 1,
b: 2,
c: 3,
};
// functions are also objects
const f = (a, b) => a + b;
// arrays are also objects
const array = [1, 2, 3, 4, 5];
// 2. Which of these statements are true
0 == "0"; // true: The string '0' will be converted to the number 0 before the comparison
0 === "0"; // false: The types are different
null == undefined; // true
false == "false"; // false: The string 'false' will be converted to the boolean true before the comparison
[] == ""; // true: The empty array will be converted to the string '' before the comparison
"" == false; // true: The string "" will be converted to the boolean false before the comparison
NaN == NaN; // false by definition
({ a: 1 } == { a: 1 }); // false, both objects are allocated at different positions in memory, and js only compares their addresses
({} === {}); // false, same as above
0.1 + 0.2 == 0.3; // false: The rounding errors due to floating point's finite precision are different on the lhs and rhs
0.2 + 0.2 == 0.4; // true: Here both sides lead to the same inprecisions
typeof [] == "array"; // false, typeof returns the string "object" for arrays
typeof {} == "object"; // true
typeof console.log == "function"; // true
typeof typeof 0 == "number"; // false: typeof 0 returns the string "number", but then the second typeof returns the string "string"
// 3. Fill in the blank
["a", "b", "c", "d", "e", "f"].slice(2, 5); // [c', 'd', 'e']
[1, 2, 3].map((x) => x * 2); // [2, 4, 6]
["Apfel", "Birne", "Aprikose", "Karotte", "Ananas"].filter((s) =>
s.startsWith("A")
); // ['Apfel', 'Aprikose', 'Ananans']
["A", "B", "B", "A"].reduce((result, next) => {
return result + "-" + next;
}); // 'A-B-B-A'
["a", "b", "c", "d", "e"].sort((a, b) => -a.localeCompare(b)); // ['e', 'd', 'c', 'b', 'a']
// 4. Name an example of a constructor function that's built into Javascript
// new Date(), new HashMap, new Array(), new String(), new Promise()
// 5. What's the difference between an arrow function and a traditional function expression?
// In arrow functions *this* is bound lexically, i.e. to the scope in which the function is defined
// In traditional functions *this* is bound dynamically, i.e. most often to the object the function is called on.
// 6. What's the difference between these two functions A and B?
async function A() {
return "Hello";
}
function B() {
return new Promise((resolve) => resolve("Hello"));
}
// There's no difference for the user, the async function A() is just syntactic sugar for the function B()
// 7. Which of these attributes/features are a part of Javascript (in its current widely used implementations)
/*
- Statically Typed -> No
- Single Threaded -> Yes
- Purely Functional -> No, functions can have side effects
- Event-Based Concurrency -> Yes
- Just in Time Compiled -> Yes
- Ahead of Time Compiled -> No (although theoretically possible)
- Parallelisable -> No, only I/O can be parallelised
- Non Blocking I/O -> Yes, there are non blocking I/O functions like fetch or fs.readFile
- Imperative -> Yes
- Object-oriented -> Yes, but using prototypes instead of classes
- Multiple-inheritance -> No, every object only has one prototype
- Dynamic Scoping -> No, apart from 'this' all variables are lexically scoped
- Lexical Scoping -> Yes
- First Class Modules -> Yes, a module can be imported as a javascript object
- Garbage Collected -> Yes
- Memory Safe -> Yes
- Thread Safe -> Yes
- Implicit Type Coercion -> Yes, e.g. at the + or == operator
- Operator Overloading -> No user defined operator overloading
- Dynamic -> Yes, all variables including functions can be dynamically reassigned during runtime
- Functional -> Yes, in the sense that functions are first class objects
*/
// 8. Use the fetch API to download a random joke from https://official-joke-api.appspot.com/random_joke
// Hint: paste the snippet into the console of your browser instead of node
// Possibility 1: Nested then
fetch("https://official-joke-api.appspot.com/random_joke").then((response) => {
response.json().then((json) => {
console.log(json);
});
});
// Possibility 2: Chained, with the then handler returning another promise
fetch("https://official-joke-api.appspot.com/random_joke")
.then((response) => {
return response.json();
})
.then((json) => {
console.log(json);
});
// Possibility 3: async await
async function getJoke() {
const response = await fetch(
"https://official-joke-api.appspot.com/random_joke"
);
const json = await response.json();
console.log(json);
}
// 9. How could you import all four functions
// math.js
const add = (a, b) => a + b;
export { add };
export function mul(a, b) {
return a * b;
}
export const sub = (a, b) => a - b;
export default function div(a, b) {
return a / b;
}
// index.js
// Possibility A: Separate lines
import { add, sub, mul } from "./math"; // importing named exports
import div from "./math"; // importing the default export
// Possibility B: one line
import div, { add, sub, mul } from "./math";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment