From ba123c9612e1ce9d225e5c40989e295a3d33e7f4 Mon Sep 17 00:00:00 2001 From: aaqibjavaid Date: Mon, 11 Nov 2024 19:59:12 +0530 Subject: [PATCH] chore: add console log --- README.md | 620 +++++++++++++++++++++++++++--------------------------- 1 file changed, 310 insertions(+), 310 deletions(-) diff --git a/README.md b/README.md index b203a060..5d1e6a0b 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ From basic to advanced: test how well you know JavaScript, refresh your knowledg function sayHi() { console.log(name); console.log(age); - var name = 'Lydia'; + var name = "Lydia"; let age = 21; } @@ -152,8 +152,8 @@ Since there is no value `radius` in the scope of the arrow function, `this.radiu ###### 4. What's the output? ```javascript -+true; -!'Lydia'; +console.log(+true); +console.log(!"Lydia"); ``` - A: `1` and `false` @@ -178,11 +178,11 @@ The string `'Lydia'` is a truthy value. What we're actually asking, is "Is this ```javascript const bird = { - size: 'small', + size: "small", }; const mouse = { - name: 'Mickey', + name: "Mickey", small: true, }; ``` @@ -213,11 +213,11 @@ However, with dot notation, this doesn't happen. `mouse` does not have a key cal ###### 6. What's the output? ```javascript -let c = { greeting: 'Hey!' }; +let c = { greeting: "Hey!" }; let d; d = c; -c.greeting = 'Hello'; +c.greeting = "Hello"; console.log(d.greeting); ``` @@ -287,13 +287,13 @@ class Chameleon { return this.newColor; } - constructor({ newColor = 'green' } = {}) { + constructor({ newColor = "green" } = {}) { this.newColor = newColor; } } -const freddie = new Chameleon({ newColor: 'purple' }); -console.log(freddie.colorChange('orange')); +const freddie = new Chameleon({ newColor: "purple" }); +console.log(freddie.colorChange("orange")); ``` - A: `orange` @@ -348,10 +348,10 @@ In order to avoid this, we can use `"use strict"`. This makes sure that you have ```javascript function bark() { - console.log('Woof!'); + console.log("Woof!"); } -bark.animal = 'dog'; +bark.animal = "dog"; ``` - A: Nothing, this is totally fine! @@ -381,8 +381,8 @@ function Person(firstName, lastName) { this.lastName = lastName; } -const member = new Person('Lydia', 'Hallie'); -Person.getFullName = function() { +const member = new Person("Lydia", "Hallie"); +Person.getFullName = function () { return `${this.firstName} ${this.lastName}`; }; @@ -399,12 +399,12 @@ console.log(member.getFullName()); #### Answer: A -In JavaScript, functions are objects, and therefore, the method `getFullName` gets added to the constructor function object itself. For that reason, we can call `Person.getFullName()`, but `member.getFullName` throws a `TypeError`. +In JavaScript, functions are objects, and therefore, the method `getFullName` gets added to the constructor function object itself. For that reason, we can call `Person.getFullName()`, but `member.getFullName` throws a `TypeError`. If you want a method to be available to all object instances, you have to add it to the prototype property: ```js -Person.prototype.getFullName = function() { +Person.prototype.getFullName = function () { return `${this.firstName} ${this.lastName}`; }; ``` @@ -422,8 +422,8 @@ function Person(firstName, lastName) { this.lastName = lastName; } -const lydia = new Person('Lydia', 'Hallie'); -const sarah = Person('Sarah', 'Smith'); +const lydia = new Person("Lydia", "Hallie"); +const sarah = Person("Sarah", "Smith"); console.log(lydia); console.log(sarah); @@ -493,7 +493,7 @@ function sum(a, b) { return a + b; } -sum(1, '2'); +sum(1, "2"); ``` - A: `NaN` @@ -560,7 +560,7 @@ function getPersonInfo(one, two, three) { console.log(three); } -const person = 'Lydia'; +const person = "Lydia"; const age = 21; getPersonInfo`${person} is ${age} years old`; @@ -587,9 +587,9 @@ If you use tagged template literals, the value of the first argument is always a ```javascript function checkAge(data) { if (data === { age: 18 }) { - console.log('You are an adult!'); + console.log("You are an adult!"); } else if (data == { age: 18 }) { - console.log('You are still an adult.'); + console.log("You are still an adult."); } else { console.log(`Hmm.. You don't have an age I guess`); } @@ -649,7 +649,7 @@ The rest parameter (`...args`) lets us "collect" all remaining arguments into an ```javascript function getAge() { - 'use strict'; + "use strict"; age = 21; console.log(age); } @@ -677,7 +677,7 @@ With `"use strict"`, you can make sure that you don't accidentally declare globa ###### 21. What's the value of `sum`? ```javascript -const sum = eval('10*10+5'); +const sum = eval("10*10+5"); ``` - A: `105` @@ -700,7 +700,7 @@ const sum = eval('10*10+5'); ###### 22. How long is cool_secret accessible? ```javascript -sessionStorage.setItem('cool_secret', 123); +sessionStorage.setItem("cool_secret", 123); ``` - A: Forever, the data doesn't get lost. @@ -753,12 +753,12 @@ You cannot do this with `let` or `const` since they're block-scoped and therefor ###### 24. What's the output? ```javascript -const obj = { 1: 'a', 2: 'b', 3: 'c' }; +const obj = { 1: "a", 2: "b", 3: "c" }; const set = new Set([1, 2, 3, 4, 5]); -obj.hasOwnProperty('1'); +obj.hasOwnProperty("1"); obj.hasOwnProperty(1); -set.has('1'); +set.has("1"); set.has(1); ``` @@ -784,7 +784,7 @@ It doesn't work that way for a set. There is no `'1'` in our set: `set.has('1')` ###### 25. What's the output? ```javascript -const obj = { a: 'one', b: 'two', a: 'three' }; +const obj = { a: "one", b: "two", a: "three" }; console.log(obj); ``` @@ -853,12 +853,12 @@ The `continue` statement skips an iteration if a certain condition returns `true ```javascript String.prototype.giveLydiaPizza = () => { - return 'Just give Lydia pizza already!'; + return "Just give Lydia pizza already!"; }; -const name = 'Lydia'; +const name = "Lydia"; -console.log(name.giveLydiaPizza()) +console.log(name.giveLydiaPizza()); ``` - A: `"Just give Lydia pizza already!"` @@ -882,8 +882,8 @@ console.log(name.giveLydiaPizza()) ```javascript const a = {}; -const b = { key: 'b' }; -const c = { key: 'c' }; +const b = { key: "b" }; +const c = { key: "c" }; a[b] = 123; a[c] = 456; @@ -915,9 +915,9 @@ Then, we log `a[b]`, which is actually `a["[object Object]"]`. We just set that ###### 30. What's the output? ```javascript -const foo = () => console.log('First'); -const bar = () => setTimeout(() => console.log('Second')); -const baz = () => console.log('Third'); +const foo = () => console.log("First"); +const bar = () => setTimeout(() => console.log("Second")); +const baz = () => console.log("Third"); bar(); foo(); @@ -970,9 +970,7 @@ This is where an event loop starts to work. An **event loop** looks at the stack ```html
- +
``` @@ -998,9 +996,7 @@ The deepest nested element that caused the event is the target of the event. You ```html
-

- Click here! -

+

Click here!

``` @@ -1024,7 +1020,7 @@ If we click `p`, we see two logs: `p` and `div`. During event propagation, there ###### 33. What's the output? ```javascript -const person = { name: 'Lydia' }; +const person = { name: "Lydia" }; function sayHi(age) { return `${this.name} is ${age}`; @@ -1074,7 +1070,6 @@ console.log(typeof sayHi()); #### Answer: B The `sayHi` function returns the returned value of the immediately invoked function expression (IIFE). This function returned `0`, which is type `"number"`. - FYI: `typeof` can return the following list of values: `undefined`, `boolean`, `number`, `bigint`, `string`, `symbol`, `function` and `object`. Note that `typeof null` returns `"object"`.

@@ -1087,8 +1082,8 @@ FYI: `typeof` can return the following list of values: `undefined`, `boolean`, ` ```javascript 0; new Number(0); -(''); -(' '); +(""); +(" "); new Boolean(false); undefined; ``` @@ -1237,11 +1232,14 @@ What differentiates a primitive from an object is that primitives do not have an ###### 40. What's the output? ```javascript -[[0, 1], [2, 3]].reduce( +[ + [0, 1], + [2, 3], +].reduce( (acc, cur) => { return acc.concat(cur); }, - [1, 2], + [1, 2] ); ``` @@ -1268,7 +1266,7 @@ Then, `[1, 2, 0, 1]` is `acc` and `[2, 3]` is `cur`. We concatenate them, and ge ```javascript !!null; -!!''; +!!""; !!1; ``` @@ -1296,7 +1294,7 @@ Then, `[1, 2, 0, 1]` is `acc` and `[2, 3]` is `cur`. We concatenate them, and ge ###### 42. What does the `setInterval` method return in the browser? ```javascript -setInterval(() => console.log('Hi'), 1000); +setInterval(() => console.log("Hi"), 1000); ``` - A: a unique id @@ -1319,7 +1317,7 @@ It returns a unique id. This id can be used to clear that interval with the `cle ###### 43. What does this return? ```javascript -[...'Lydia']; +[..."Lydia"]; ``` - A: `["L", "y", "d", "i", "a"]` @@ -1378,14 +1376,14 @@ Then, we invoke the function again with the `next()` method. It starts to contin ```javascript const firstPromise = new Promise((res, rej) => { - setTimeout(res, 500, 'one'); + setTimeout(res, 500, "one"); }); const secondPromise = new Promise((res, rej) => { - setTimeout(res, 100, 'two'); + setTimeout(res, 100, "two"); }); -Promise.race([firstPromise, secondPromise]).then(res => console.log(res)); +Promise.race([firstPromise, secondPromise]).then((res) => console.log(res)); ``` - A: `"one"` @@ -1408,7 +1406,7 @@ When we pass multiple promises to the `Promise.race` method, it resolves/rejects ###### 46. What's the output? ```javascript -let person = { name: 'Lydia' }; +let person = { name: "Lydia" }; const members = [person]; person = null; @@ -1448,7 +1446,7 @@ We are only modifying the value of the `person` variable, and not the first elem ```javascript const person = { - name: 'Lydia', + name: "Lydia", age: 21, }; @@ -1477,7 +1475,7 @@ With a `for-in` loop, we can iterate through object keys, in this case `name` an ###### 48. What's the output? ```javascript -console.log(3 + 4 + '5'); +console.log(3 + 4 + "5"); ``` - A: `"345"` @@ -1504,7 +1502,7 @@ Operator associativity is the order in which the compiler evaluates the expressi ###### 49. What's the value of `num`? ```javascript -const num = parseInt('7*6', 10); +const num = parseInt("7*6", 10); ``` - A: `42` @@ -1529,8 +1527,8 @@ Only the first number in the string is returned. Based on the _radix_ (the secon ###### 50. What's the output? ```javascript -[1, 2, 3].map(num => { - if (typeof num === 'number') return; +[1, 2, 3].map((num) => { + if (typeof num === "number") return; return num * 2; }); ``` @@ -1558,12 +1556,12 @@ However, we don’t return a value. When we don’t return a value from the func ```javascript function getInfo(member, year) { - member.name = 'Lydia'; - year = '1998'; + member.name = "Lydia"; + year = "1998"; } -const person = { name: 'Sarah' }; -const birthYear = '1997'; +const person = { name: "Sarah" }; +const birthYear = "1997"; getInfo(person, birthYear); @@ -1595,15 +1593,15 @@ The value of `person` is an object. The argument `member` has a (copied) referen ```javascript function greeting() { - throw 'Hello world!'; + throw "Hello world!"; } function sayHi() { try { const data = greeting(); - console.log('It worked!', data); + console.log("It worked!", data); } catch (e) { - console.log('Oh no an error:', e); + console.log("Oh no an error:", e); } } @@ -1633,8 +1631,8 @@ With the `catch` statement, we can specify what to do if an exception is thrown ```javascript function Car() { - this.make = 'Lamborghini'; - return { make: 'Maserati' }; + this.make = "Lamborghini"; + return { make: "Maserati" }; } const myCar = new Car(); @@ -1708,11 +1706,11 @@ class Dog { } } -Dog.prototype.bark = function() { +Dog.prototype.bark = function () { console.log(`Woof I am ${this.name}`); }; -const pet = new Dog('Mara'); +const pet = new Dog("Mara"); pet.bark(); @@ -1777,7 +1775,7 @@ export default counter; ```javascript // index.js -import myCounter from './counter'; +import myCounter from "./counter"; myCounter += 1; @@ -1806,7 +1804,7 @@ When we try to increment the value of `myCounter`, it throws an error: `myCounte ###### 58. What's the output? ```javascript -const name = 'Lydia'; +const name = "Lydia"; age = 21; console.log(delete name); @@ -1877,7 +1875,7 @@ This means that the value of `y` is equal to the first value in the array, which ###### 60. What's the output? ```javascript -const user = { name: 'Lydia', age: 21 }; +const user = { name: "Lydia", age: 21 }; const admin = { admin: true, ...user }; console.log(admin); @@ -1903,9 +1901,9 @@ It's possible to combine objects using the spread operator `...`. It lets you cr ###### 61. What's the output? ```javascript -const person = { name: 'Lydia' }; +const person = { name: "Lydia" }; -Object.defineProperty(person, 'age', { value: 21 }); +Object.defineProperty(person, "age", { value: 21 }); console.log(person); console.log(Object.keys(person)); @@ -1934,12 +1932,12 @@ Properties added using the `defineProperty` method are immutable by default. You ```javascript const settings = { - username: 'lydiahallie', + username: "lydiahallie", level: 19, health: 90, }; -const data = JSON.stringify(settings, ['level', 'health']); +const data = JSON.stringify(settings, ["level", "health"]); console.log(data); ``` @@ -1970,7 +1968,7 @@ If the replacer is a _function_, this function gets called on every property in let num = 10; const increaseNumber = () => num++; -const increasePassedNumber = number => number++; +const increasePassedNumber = (number) => number++; const num1 = increaseNumber(); const num2 = increasePassedNumber(num1); @@ -2127,12 +2125,12 @@ The `Labrador` class receives two arguments, `name` since it extends `Dog`, and ```javascript // index.js -console.log('running index.js'); -import { sum } from './sum.js'; +console.log("running index.js"); +import { sum } from "./sum.js"; console.log(sum(1, 2)); // sum.js -console.log('running sum.js'); +console.log("running sum.js"); export const sum = (a, b) => a + b; ``` @@ -2160,7 +2158,7 @@ This is a difference between `require()` in CommonJS and `import`! With `require ```javascript console.log(Number(2) === Number(2)); console.log(Boolean(false) === Boolean(false)); -console.log(Symbol('foo') === Symbol('foo')); +console.log(Symbol("foo") === Symbol("foo")); ``` - A: `true`, `true`, `false` @@ -2183,7 +2181,7 @@ Every Symbol is entirely unique. The purpose of the argument passed to the Symbo ###### 69. What's the output? ```javascript -const name = 'Lydia Hallie'; +const name = "Lydia Hallie"; console.log(name.padStart(13)); console.log(name.padStart(2)); ``` @@ -2210,7 +2208,7 @@ If the argument passed to the `padStart` method is smaller than the length of th ###### 70. What's the output? ```javascript -console.log('πŸ₯‘' + 'πŸ’»'); +console.log("πŸ₯‘" + "πŸ’»"); ``` - A: `"πŸ₯‘πŸ’»"` @@ -2234,11 +2232,11 @@ With the `+` operator, you can concatenate strings. In this case, we are concate ```javascript function* startGame() { - const answer = yield 'Do you love JavaScript?'; - if (answer !== 'Yes') { + const answer = yield "Do you love JavaScript?"; + if (answer !== "Yes") { return "Oh wow... Guess we're done here"; } - return 'JavaScript loves you back ❀️'; + return "JavaScript loves you back ❀️"; } const game = startGame(); @@ -2306,7 +2304,7 @@ In this case, the string is `Hello\nworld`, which gets logged. ```javascript async function getData() { - return await Promise.resolve('I made it!'); + return await Promise.resolve("I made it!"); } const data = getData(); @@ -2343,7 +2341,7 @@ function addToList(item, list) { return list.push(item); } -const result = addToList('apple', ['banana']); +const result = addToList("apple", ["banana"]); console.log(result); ``` @@ -2403,7 +2401,7 @@ Since `shape` is frozen, and since the value of `x` is not an object, we cannot ###### 76. What's the output? ```javascript -const { firstName: myName } = { firstName: 'Lydia' }; +const { firstName: myName } = { firstName: "Lydia" }; console.log(firstName); ``` @@ -2421,7 +2419,7 @@ console.log(firstName); By using [destructuring assignment](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) syntax we can unpack values from arrays, or properties from objects, into distinct variables: ```javascript -const { firstName } = { firstName: 'Lydia' }; +const { firstName } = { firstName: "Lydia" }; // ES5 version: // var firstName = { firstName: 'Lydia' }.firstName; @@ -2431,7 +2429,7 @@ console.log(firstName); // "Lydia" Also, a property can be unpacked from an object and assigned to a variable with a different name than the object property: ```javascript -const { firstName: myName } = { firstName: 'Lydia' }; +const { firstName: myName } = { firstName: "Lydia" }; // ES5 version: // var myName = { firstName: 'Lydia' }.firstName; @@ -2444,12 +2442,11 @@ Therefore, `firstName` does not exist as a variable, thus attempting to access i **Note:** Be aware of the `global scope` properties: ```javascript -const { name: myName } = { name: 'Lydia' }; +const { name: myName } = { name: "Lydia" }; console.log(myName); // "lydia" console.log(name); // "" ----- Browser e.g. Chrome console.log(name); // ReferenceError: name is not defined ----- NodeJS - ``` Whenever Javascript is unable to find a variable within the _current scope_, it climbs up the [Scope chain](https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/ch3.md) and searches for it and if it reaches the top-level scope, aka **Global scope**, and still doesn't find it, it will throw a `ReferenceError`. @@ -2493,7 +2490,7 @@ The `sum` function always returns the same result. If we pass `1` and `2`, it wi ```javascript const add = () => { const cache = {}; - return num => { + return (num) => { if (num in cache) { return `From cache! ${cache[num]}`; } else { @@ -2538,7 +2535,7 @@ The third time, we pass `5 * 2` to the function which gets evaluated to `10`. Th ###### 79. What is the output? ```javascript -const myLifeSummedUp = ['β˜•', 'πŸ’»', '🍷', '🍫']; +const myLifeSummedUp = ["β˜•", "πŸ’»", "🍷", "🍫"]; for (let item in myLifeSummedUp) { console.log(item); @@ -2634,13 +2631,13 @@ In this case, if we didn't pass a value or if we passed `undefined`, `name` woul ###### 82. What is the output? ```javascript -var status = '😎'; +var status = "😎"; setTimeout(() => { - const status = '😍'; + const status = "😍"; const data = { - status: 'πŸ₯‘', + status: "πŸ₯‘", getStatus() { return this.status; }, @@ -2674,12 +2671,12 @@ With the `call` method, we can change the object to which the `this` keyword ref ```javascript const person = { - name: 'Lydia', + name: "Lydia", age: 21, }; let city = person.city; -city = 'Amsterdam'; +city = "Amsterdam"; console.log(person); ``` @@ -2743,9 +2740,9 @@ Variables with the `const` and `let` keywords are _block-scoped_. A block is any ###### 85. What kind of information would get logged? ```javascript -fetch('https://www.website.com/api/user/1') - .then(res => res.json()) - .then(res => console.log(res)); +fetch("https://www.website.com/api/user/1") + .then((res) => res.json()) + .then((res) => console.log(res)); ``` - A: The result of the `fetch` method. @@ -2799,7 +2796,7 @@ By setting `hasName` equal to `name`, you set `hasName` equal to whatever value ###### 87. What's the output? ```javascript -console.log('I want pizza'[0]); +console.log("I want pizza"[0]); ``` - A: `"""` @@ -2854,11 +2851,11 @@ If you're trying to set a default parameter's value equal to a parameter that is ```javascript // module.js -export default () => 'Hello world'; -export const name = 'Lydia'; +export default () => "Hello world"; +export const name = "Lydia"; // index.js -import * as data from './module'; +import * as data from "./module"; console.log(data); ``` @@ -2891,7 +2888,7 @@ class Person { } } -const member = new Person('John'); +const member = new Person("John"); console.log(typeof member); ``` @@ -2951,7 +2948,7 @@ Then, we try to use the `.push` method on `newList`. Since `newList` is the nume ```javascript function giveLydiaPizza() { - return 'Here is pizza!'; + return "Here is pizza!"; } const giveLydiaChocolate = () => @@ -2982,7 +2979,7 @@ Regular functions, such as the `giveLydiaPizza` function, have a `prototype` pro ```javascript const person = { - name: 'Lydia', + name: "Lydia", age: 21, }; @@ -3042,7 +3039,7 @@ function getItems(fruitList, favoriteFruit, ...args) { return [...fruitList, ...args, favoriteFruit]; } -getItems(['banana', 'apple'], 'pear', 'orange'); +getItems(["banana", "apple"], "pear", "orange"); ``` The above example works. This returns the array `[ 'banana', 'apple', 'orange', 'pear' ]` @@ -3056,9 +3053,9 @@ The above example works. This returns the array `[ 'banana', 'apple', 'orange', ```javascript function nums(a, b) { - if (a > b) console.log('a is bigger'); - else console.log('b is bigger'); - return + if (a > b) console.log("a is bigger"); + else console.log("b is bigger"); + return; a + b; } @@ -3097,13 +3094,13 @@ This means that `a + b` is never reached, since a function stops running after t ```javascript class Person { constructor() { - this.name = 'Lydia'; + this.name = "Lydia"; } } Person = class AnotherPerson { constructor() { - this.name = 'Sarah'; + this.name = "Sarah"; } }; @@ -3132,7 +3129,7 @@ We can set classes equal to other classes/function constructors. In this case, w ```javascript const info = { - [Symbol('a')]: 'b', + [Symbol("a")]: "b", }; console.log(info); @@ -3187,7 +3184,7 @@ The `getList` function receives an array as its argument. Between the parenthese With the rest parameter `...y`, we put all "remaining" arguments in an array. The remaining arguments are `2`, `3` and `4` in this case. The value of `y` is an array, containing all the rest parameters. The value of `x` is equal to `1` in this case, so when we log `[x, y]`, `[1, [2, 3, 4]]` gets logged. -The `getUser` function receives an object. With arrow functions, we don't _have_ to write curly brackets if we just return one value. However, if you want to instantly return an _object_ from an arrow function, you have to write it between parentheses, otherwise everything between the two braces will be interpreted as a block statement. In this case the code between the braces is not a valid JavaScript code, so a `SyntaxError` gets thrown. +The `getUser` function receives an object. With arrow functions, we don't _have_ to write curly brackets if we just return one value. However, if you want to instantly return an _object_ from an arrow function, you have to write it between parentheses, otherwise everything between the two braces will be interpreted as a block statement. In this case the code between the braces is not a valid JavaScript code, so a `SyntaxError` gets thrown. The following function would have returned an object: @@ -3201,7 +3198,7 @@ The following function would have returned an object: ###### 99. What's the output? ```javascript -const name = 'Lydia'; +const name = "Lydia"; console.log(name()); ``` @@ -3233,8 +3230,8 @@ ReferenceErrors get thrown when JavaScript isn't able to find a reference to a v ```javascript // πŸŽ‰βœ¨ This is my 100th question! βœ¨πŸŽ‰ -const output = `${[] && 'Im'}possible! -You should${'' && `n't`} see a therapist after so much JavaScript lol`; +const output = `${[] && "Im"}possible! +You should${"" && `n't`} see a therapist after so much JavaScript lol`; ``` - A: `possible! You should see a therapist after so much JavaScript lol` @@ -3260,7 +3257,7 @@ You should${'' && `n't`} see a therapist after so much JavaScript lol`; ```javascript const one = false || {} || null; -const two = null || false || ''; +const two = null || false || ""; const three = [] || 0 || true; console.log(one, two, three); @@ -3292,16 +3289,16 @@ With the `||` operator, we can return the first truthy operand. If all values ar ###### 102. What's the value of output? ```javascript -const myPromise = () => Promise.resolve('I have resolved!'); +const myPromise = () => Promise.resolve("I have resolved!"); function firstFunction() { - myPromise().then(res => console.log(res)); - console.log('second'); + myPromise().then((res) => console.log(res)); + console.log("second"); } async function secondFunction() { console.log(await myPromise()); - console.log('second'); + console.log("second"); } firstFunction(); @@ -3339,8 +3336,8 @@ This means that it waited for the `myPromise` to resolve with the value `I have const set = new Set(); set.add(1); -set.add('Lydia'); -set.add({ name: 'Lydia' }); +set.add("Lydia"); +set.add({ name: "Lydia" }); for (let item of set) { console.log(item + 2); @@ -3400,13 +3397,13 @@ In this case, we just passed the numerical value `5`. It returns a resolved prom ```javascript function compareMembers(person1, person2 = person) { if (person1 !== person2) { - console.log('Not the same!'); + console.log("Not the same!"); } else { - console.log('They are the same!'); + console.log("They are the same!"); } } -const person = { name: 'Lydia' }; +const person = { name: "Lydia" }; compareMembers(person); ``` @@ -3445,7 +3442,7 @@ const colorConfig = { yellow: false, }; -const colors = ['pink', 'red', 'blue']; +const colors = ["pink", "red", "blue"]; console.log(colorConfig.colors[1]); ``` @@ -3474,7 +3471,7 @@ JavaScript interprets (or unboxes) statements. When we use bracket notation, it ###### 107. What's its value? ```javascript -console.log('❀️' === '❀️'); +console.log("❀️" === "❀️"); ``` - A: `true` @@ -3495,14 +3492,14 @@ Under the hood, emojis are unicodes. The unicodes for the heart emoji is `"U+276 ###### 108. Which of these methods modifies the original array? ```javascript -const emojis = ['✨', 'πŸ₯‘', '😍']; +const emojis = ["✨", "πŸ₯‘", "😍"]; -emojis.map(x => x + '✨'); -emojis.filter(x => x !== 'πŸ₯‘'); -emojis.find(x => x !== 'πŸ₯‘'); -emojis.reduce((acc, cur) => acc + '✨'); -emojis.slice(1, 2, '✨'); -emojis.splice(1, 2, '✨'); +emojis.map((x) => x + "✨"); +emojis.filter((x) => x !== "πŸ₯‘"); +emojis.find((x) => x !== "πŸ₯‘"); +emojis.reduce((acc, cur) => acc + "✨"); +emojis.slice(1, 2, "✨"); +emojis.splice(1, 2, "✨"); ``` - A: `All of them` @@ -3527,10 +3524,10 @@ With `splice` method, we modify the original array by deleting, replacing or add ###### 109. What's the output? ```javascript -const food = ['πŸ•', '🍫', 'πŸ₯‘', 'πŸ”']; +const food = ["πŸ•", "🍫", "πŸ₯‘", "πŸ”"]; const info = { favoriteFood: food[0] }; -info.favoriteFood = '🍝'; +info.favoriteFood = "🍝"; console.log(food); ``` @@ -3584,7 +3581,7 @@ const jsonArray = JSON.stringify([1, 2, 3]); // '[1, 2, 3]' JSON.parse(jsonArray); // [1, 2, 3] // Stringifying an object into valid JSON, then parsing the JSON string to a JavaScript value: -const jsonArray = JSON.stringify({ name: 'Lydia' }); // '{"name":"Lydia"}' +const jsonArray = JSON.stringify({ name: "Lydia" }); // '{"name":"Lydia"}' JSON.parse(jsonArray); // { name: 'Lydia' } ``` @@ -3596,11 +3593,11 @@ JSON.parse(jsonArray); // { name: 'Lydia' } ###### 111. What's the output? ```javascript -let name = 'Lydia'; +let name = "Lydia"; function getName() { console.log(name); - let name = 'Sarah'; + let name = "Sarah"; } getName(); @@ -3623,7 +3620,7 @@ Variables with the `let` keyword (and `const`) are hoisted, but unlike `var`, do If we wouldn't have declared the `name` variable within the `getName` function, the javascript engine would've looked down the _scope chain_. The outer scope has a variable called `name` with the value of `Lydia`. In that case, it would've logged `Lydia`. ```javascript -let name = 'Lydia'; +let name = "Lydia"; function getName() { console.log(name); @@ -3641,11 +3638,11 @@ getName(); // Lydia ```javascript function* generatorOne() { - yield ['a', 'b', 'c']; + yield ["a", "b", "c"]; } function* generatorTwo() { - yield* ['a', 'b', 'c']; + yield* ["a", "b", "c"]; } const one = generatorOne(); @@ -3691,7 +3688,7 @@ console.log(two.next().value); // undefined ###### 113. What's the output? ```javascript -console.log(`${(x => x)('I love')} to program`); +console.log(`${((x) => x)("I love")} to program`); ``` - A: `I love to program` @@ -3716,7 +3713,7 @@ Expressions within template literals are evaluated first. This means that the st ```javascript let config = { alert: setInterval(() => { - console.log('Alert!'); + console.log("Alert!"); }, 1000), }; @@ -3733,9 +3730,9 @@ config = null; #### Answer: C -Normally when we set objects equal to `null`, those objects get _garbage collected_ as there is no reference anymore to that object. However, since the callback function within `setInterval` is an arrow function (thus bound to the `config` object), the callback function still holds a reference to the `config` object. -As long as there is a reference, the object won't get garbage collected. -Since this is an interval, setting `config` to `null` or `delete`-ing `config.alert` won't garbage-collect the interval, so the interval will still be called. +Normally when we set objects equal to `null`, those objects get _garbage collected_ as there is no reference anymore to that object. However, since the callback function within `setInterval` is an arrow function (thus bound to the `config` object), the callback function still holds a reference to the `config` object. +As long as there is a reference, the object won't get garbage collected. +Since this is an interval, setting `config` to `null` or `delete`-ing `config.alert` won't garbage-collect the interval, so the interval will still be called. It should be cleared with `clearInterval(config.alert)` to remove it from memory. Since it was not cleared, the `setInterval` callback function will still get invoked every 1000ms (1s). @@ -3748,16 +3745,16 @@ Since it was not cleared, the `setInterval` callback function will still get inv ```javascript const myMap = new Map(); -const myFunc = () => 'greeting'; +const myFunc = () => "greeting"; -myMap.set(myFunc, 'Hello world!'); +myMap.set(myFunc, "Hello world!"); //1 -myMap.get('greeting'); +myMap.get("greeting"); //2 myMap.get(myFunc); //3 -myMap.get(() => 'greeting'); +myMap.get(() => "greeting"); ``` - A: 1 @@ -3784,14 +3781,14 @@ When adding a key/value pair using the `set` method, the key will be the value o ```javascript const person = { - name: 'Lydia', + name: "Lydia", age: 21, }; const changeAge = (x = { ...person }) => (x.age += 1); const changeAgeAndName = (x = { ...person }) => { x.age += 1; - x.name = 'Sarah'; + x.name = "Sarah"; }; changeAge(person); @@ -3850,7 +3847,7 @@ With the spread operator `...`, we can _spread_ iterables to individual elements ```javascript let num = 1; -const list = ['πŸ₯³', '🀠', 'πŸ₯°', 'πŸ€ͺ']; +const list = ["πŸ₯³", "🀠", "πŸ₯°", "πŸ€ͺ"]; console.log(list[(num += 1)]); ``` @@ -3876,11 +3873,11 @@ With the `+=` operator, we're incrementing the value of `num` by `1`. `num` had ```javascript const person = { - firstName: 'Lydia', - lastName: 'Hallie', + firstName: "Lydia", + lastName: "Hallie", pet: { - name: 'Mara', - breed: 'Dutch Tulip Hound', + name: "Mara", + breed: "Dutch Tulip Hound", }, getFullName() { return `${this.firstName} ${this.lastName}`; @@ -3918,10 +3915,10 @@ With the optional chaining operator `?.`, we no longer have to explicitly check ###### 120. What's the output? ```javascript -const groceries = ['banana', 'apple', 'peanuts']; +const groceries = ["banana", "apple", "peanuts"]; -if (groceries.indexOf('banana')) { - console.log('We have to buy bananas!'); +if (groceries.indexOf("banana")) { + console.log("We have to buy bananas!"); } else { console.log(`We don't have to buy bananas!`); } @@ -3977,10 +3974,10 @@ The `language` method is a `setter`. Setters don't hold an actual value, their p ###### 122. What's the output? ```javascript -const name = 'Lydia Hallie'; +const name = "Lydia Hallie"; -console.log(!typeof name === 'object'); -console.log(!typeof name === 'string'); +console.log(!typeof name === "object"); +console.log(!typeof name === "string"); ``` - A: `false` `true` @@ -4005,7 +4002,7 @@ console.log(!typeof name === 'string'); ###### 123. What's the output? ```javascript -const add = x => y => z => { +const add = (x) => (y) => (z) => { console.log(x, y, z); return x + y + z; }; @@ -4095,20 +4092,20 @@ myFunc(1, 2, 3); ```javascript function getFine(speed, amount) { - const formattedSpeed = new Intl.NumberFormat('en-US', { - style: 'unit', - unit: 'mile-per-hour' + const formattedSpeed = new Intl.NumberFormat("en-US", { + style: "unit", + unit: "mile-per-hour", }).format(speed); - const formattedAmount = new Intl.NumberFormat('en-US', { - style: 'currency', - currency: 'USD' + const formattedAmount = new Intl.NumberFormat("en-US", { + style: "currency", + currency: "USD", }).format(amount); return `The driver drove ${formattedSpeed} and has to pay ${formattedAmount}`; } -console.log(getFine(130, 300)) +console.log(getFine(130, 300)); ``` - A: The driver drove 130 and has to pay 300 @@ -4131,8 +4128,8 @@ With the `Intl.NumberFormat` method, we can format numeric values to any locale. ###### 127. What's the output? ```javascript -const spookyItems = ['πŸ‘»', 'πŸŽƒ', 'πŸ•Έ']; -({ item: spookyItems[3] } = { item: 'πŸ’€' }); +const spookyItems = ["πŸ‘»", "πŸŽƒ", "πŸ•Έ"]; +({ item: spookyItems[3] } = { item: "πŸ’€" }); console.log(spookyItems); ``` @@ -4157,7 +4154,7 @@ By destructuring objects, we can unpack values from the right-hand object, and a ###### 128. What's the output? ```javascript -const name = 'Lydia Hallie'; +const name = "Lydia Hallie"; const age = 21; console.log(Number.isNaN(name)); @@ -4193,7 +4190,7 @@ const randomValue = 21; function getInfo() { console.log(typeof randomValue); - const randomValue = 'Lydia Hallie'; + const randomValue = "Lydia Hallie"; } getInfo(); @@ -4219,7 +4216,7 @@ Variables declared with the `const` keyword are not referenceable before their i ###### 130. What's the output? ```javascript -const myPromise = Promise.resolve('Woah some cool data'); +const myPromise = Promise.resolve("Woah some cool data"); (async () => { try { @@ -4227,7 +4224,7 @@ const myPromise = Promise.resolve('Woah some cool data'); } catch { throw new Error(`Oops didn't work`); } finally { - console.log('Oh finally!'); + console.log("Oh finally!"); } })(); ``` @@ -4252,7 +4249,7 @@ In the `try` block, we're logging the awaited value of the `myPromise` variable: ###### 131. What's the output? ```javascript -const emojis = ['πŸ₯‘', ['✨', '✨', ['πŸ•', 'πŸ•']]]; +const emojis = ["πŸ₯‘", ["✨", "✨", ["πŸ•", "πŸ•"]]]; console.log(emojis.flat(1)); ``` @@ -4325,19 +4322,19 @@ We invoke `counterTwo.increment()`, which sets `count` to `3`. Then, we log the ###### 133. What's the output? ```javascript -const myPromise = Promise.resolve(Promise.resolve('Promise')); +const myPromise = Promise.resolve(Promise.resolve("Promise")); function funcOne() { - setTimeout(() => console.log('Timeout 1!'), 0); - myPromise.then(res => res).then(res => console.log(`${res} 1!`)); - console.log('Last line 1!'); + setTimeout(() => console.log("Timeout 1!"), 0); + myPromise.then((res) => res).then((res) => console.log(`${res} 1!`)); + console.log("Last line 1!"); } async function funcTwo() { const res = await myPromise; - console.log(`${res} 2!`) - setTimeout(() => console.log('Timeout 2!'), 0); - console.log('Last line 2!'); + console.log(`${res} 2!`); + setTimeout(() => console.log("Timeout 2!"), 0); + console.log("Last line 2!"); } funcOne(); @@ -4358,7 +4355,7 @@ First, we invoke `funcOne`. On the first line of `funcOne`, we call the _asynchr Then we call the `myPromise` promise, which is an _asynchronous_ operation. Pay attention, that now only the first then clause was added to the microtask queue. -Both the promise and the timeout are asynchronous operations, the function keeps on running while it's busy completing the promise and handling the `setTimeout` callback. This means that `Last line 1!` gets logged first, since this is not an asynchonous operation. +Both the promise and the timeout are asynchronous operations, the function keeps on running while it's busy completing the promise and handling the `setTimeout` callback. This means that `Last line 1!` gets logged first, since this is not an asynchonous operation. Since the callstack is not empty yet, the `setTimeout` function and promise in `funcOne` cannot get added to the callstack yet. @@ -4382,7 +4379,7 @@ export default function sum(x) { } // index.js -import * as sum from './sum'; +import * as sum from "./sum"; ``` - A: `sum(4)` @@ -4399,12 +4396,12 @@ With the asterisk `*`, we import all exported values from that file, both defaul ```javascript // info.js -export const name = 'Lydia'; +export const name = "Lydia"; export const age = 21; -export default 'I love JavaScript'; +export default "I love JavaScript"; // index.js -import * as info from './info'; +import * as info from "./info"; console.log(info); ``` @@ -4435,13 +4432,13 @@ We can invoke this function, by calling `sum.default` ```javascript const handler = { - set: () => console.log('Added a new property!'), - get: () => console.log('Accessed a property!'), + set: () => console.log("Added a new property!"), + get: () => console.log("Accessed a property!"), }; const person = new Proxy({}, handler); -person.name = 'Lydia'; +person.name = "Lydia"; person.name; ``` @@ -4471,7 +4468,7 @@ Then, we access a property value on the proxy object, and the `get` property on ###### 136. Which of the following will modify the `person` object? ```javascript -const person = { name: 'Lydia Hallie' }; +const person = { name: "Lydia Hallie" }; Object.seal(person); ``` @@ -4499,9 +4496,9 @@ However, you can still modify the value of existing properties. ```javascript const person = { - name: 'Lydia Hallie', + name: "Lydia Hallie", address: { - street: '100 Main St', + street: "100 Main St", }, }; @@ -4530,7 +4527,7 @@ However, it only _shallowly_ freezes the object, meaning that only _direct_ prop ###### 138. What's the output? ```javascript -const add = x => x + x; +const add = (x) => x + x; function myFunc(num = 2, value = add(num)) { console.log(num, value); @@ -4563,21 +4560,21 @@ Then, we invoked `myFunc(3)` and passed the value `3` as the value for the argum ```javascript class Counter { - #number = 10 + #number = 10; increment() { - this.#number++ + this.#number++; } getNum() { - return this.#number + return this.#number; } } -const counter = new Counter() -counter.increment() +const counter = new Counter(); +counter.increment(); -console.log(counter.#number) +console.log(counter.#number); ``` - A: `10` @@ -4601,8 +4598,8 @@ In ES2020, we can add private variables in classes by using the `#`. We cannot a ```javascript const teams = [ - { name: 'Team 1', members: ['Paul', 'Lisa'] }, - { name: 'Team 2', members: ['Laura', 'Tim'] }, + { name: "Team 1", members: ["Paul", "Lisa"] }, + { name: "Team 2", members: ["Laura", "Tim"] }, ]; function* getMembers(members) { @@ -4645,8 +4642,8 @@ If we would've written `yield`, `return yield`, or `return`, the entire generato ```javascript const person = { - name: 'Lydia Hallie', - hobbies: ['coding'], + name: "Lydia Hallie", + hobbies: ["coding"], }; function addHobby(hobby, hobbies = person.hobbies) { @@ -4654,9 +4651,9 @@ function addHobby(hobby, hobbies = person.hobbies) { return hobbies; } -addHobby('running', []); -addHobby('dancing'); -addHobby('baking', person.hobbies); +addHobby("running", []); +addHobby("dancing"); +addHobby("baking", person.hobbies); console.log(person.hobbies); ``` @@ -4725,11 +4722,11 @@ We create the variable `pet` which is an instance of the `Flamingo` class. When ###### 143. Which of the options result(s) in an error? ```javascript -const emojis = ['πŸŽ„', 'πŸŽ…πŸΌ', '🎁', '⭐']; +const emojis = ["πŸŽ„", "πŸŽ…πŸΌ", "🎁", "⭐"]; -/* 1 */ emojis.push('🦌'); +/* 1 */ emojis.push("🦌"); /* 2 */ emojis.splice(0, 2); -/* 3 */ emojis = [...emojis, 'πŸ₯‚']; +/* 3 */ emojis = [...emojis, "πŸ₯‚"]; /* 4 */ emojis.length = 0; ``` @@ -4784,11 +4781,11 @@ Objects aren't iterable by default. An iterable is an iterable if the iterator p let count = 0; const nums = [0, 1, 2, 3]; -nums.forEach(num => { - if (num) count += 1 -}) +nums.forEach((num) => { + if (num) count += 1; +}); -console.log(count) +console.log(count); ``` - A: 1 @@ -4812,12 +4809,12 @@ The `if` condition within the `forEach` loop checks whether the value of `num` i ```javascript function getFruit(fruits) { - console.log(fruits?.[1]?.[1]) + console.log(fruits?.[1]?.[1]); } -getFruit([['🍊', '🍌'], ['🍍']]) -getFruit() -getFruit([['🍍'], ['🍊', '🍌']]) +getFruit([["🍊", "🍌"], ["🍍"]]); +getFruit(); +getFruit([["🍍"], ["🍊", "🍌"]]); ``` - A: `null`, `undefined`, 🍌 @@ -4830,11 +4827,11 @@ getFruit([['🍍'], ['🍊', '🍌']]) #### Answer: D -The `?` allows us to optionally access deeper nested properties within objects. We're trying to log the item on index `1` within the subarray that's on index `1` of the `fruits` array. If the subarray on index `1` in the `fruits` array doesn't exist, it'll simply return `undefined`. If the subarray on index `1` in the `fruits` array exists, but this subarray doesn't have an item on its `1` index, it'll also return `undefined`. +The `?` allows us to optionally access deeper nested properties within objects. We're trying to log the item on index `1` within the subarray that's on index `1` of the `fruits` array. If the subarray on index `1` in the `fruits` array doesn't exist, it'll simply return `undefined`. If the subarray on index `1` in the `fruits` array exists, but this subarray doesn't have an item on its `1` index, it'll also return `undefined`. First, we're trying to log the second item in the `['🍍']` subarray of `[['🍊', '🍌'], ['🍍']]`. This subarray only contains one item, which means there is no item on index `1`, and returns `undefined`. -Then, we're invoking the `getFruits` function without passing a value as an argument, which means that `fruits` has a value of `undefined` by default. Since we're conditionally chaining the item on index `1` of`fruits`, it returns `undefined` since this item on index `1` does not exist. +Then, we're invoking the `getFruits` function without passing a value as an argument, which means that `fruits` has a value of `undefined` by default. Since we're conditionally chaining the item on index `1` of`fruits`, it returns `undefined` since this item on index `1` does not exist. Lastly, we're trying to log the second item in the `['🍊', '🍌']` subarray of `['🍍'], ['🍊', '🍌']`. The item on index `1` within this subarray is `🍌`, which gets logged. @@ -4847,19 +4844,19 @@ Lastly, we're trying to log the second item in the `['🍊', '🍌']` subarray o ```javascript class Calc { - constructor() { - this.count = 0 - } + constructor() { + this.count = 0; + } - increase() { - this.count++ - } + increase() { + this.count++; + } } -const calc = new Calc() -new Calc().increase() +const calc = new Calc(); +new Calc().increase(); -console.log(calc.count) +console.log(calc.count); ``` - A: `0` @@ -4883,25 +4880,25 @@ We set the variable `calc` equal to a new instance of the `Calc` class. Then, we ```javascript const user = { - email: "e@mail.com", - password: "12345" -} + email: "e@mail.com", + password: "12345", +}; const updateUser = ({ email, password }) => { - if (email) { - Object.assign(user, { email }) - } + if (email) { + Object.assign(user, { email }); + } - if (password) { - user.password = password - } + if (password) { + user.password = password; + } - return user -} + return user; +}; -const updatedUser = updateUser({ email: "new@email.com" }) +const updatedUser = updateUser({ email: "new@email.com" }); -console.log(updatedUser === user) +console.log(updatedUser === user); ``` - A: `false` @@ -4924,13 +4921,13 @@ The `updateUser` function updates the values of the `email` and `password` prope ###### 149. What's the output? ```javascript -const fruit = ['🍌', '🍊', '🍎'] +const fruit = ["🍌", "🍊", "🍎"]; -fruit.slice(0, 1) -fruit.splice(0, 1) -fruit.unshift('πŸ‡') +fruit.slice(0, 1); +fruit.splice(0, 1); +fruit.unshift("πŸ‡"); -console.log(fruit) +console.log(fruit); ``` - A: `['🍌', '🍊', '🍎']` @@ -4945,7 +4942,7 @@ console.log(fruit) First, we invoke the `slice` method on the fruit array. The slice method does not modify the original array, but returns the value that it sliced off the array: the banana emoji. Then, we invoke the `splice` method on the fruit array. The splice method does modify the original array, which means that the fruit array now consists of `['🍊', '🍎']`. -At last, we invoke the `unshift` method on the `fruit` array, which modifies the original array by adding the provided value, β€˜πŸ‡β€™ in this case, as the first element in the array. The fruit array now consists of `['πŸ‡', '🍊', '🍎']`. +At last, we invoke the `unshift` method on the `fruit` array, which modifies the original array by adding the provided value, β€˜πŸ‡β€™ in this case, as the first element in the array. The fruit array now consists of `['πŸ‡', '🍊', '🍎']`.

@@ -4956,13 +4953,13 @@ At last, we invoke the `unshift` method on the `fruit` array, which modifies the ```javascript const animals = {}; -let dog = { emoji: '🐢' } -let cat = { emoji: '🐈' } +let dog = { emoji: "🐢" }; +let cat = { emoji: "🐈" }; -animals[dog] = { ...dog, name: "Mara" } -animals[cat] = { ...cat, name: "Sara" } +animals[dog] = { ...dog, name: "Mara" }; +animals[cat] = { ...cat, name: "Sara" }; -console.log(animals[dog]) +console.log(animals[dog]); ``` - A: `{ emoji: "🐢", name: "Mara" }` @@ -4975,11 +4972,11 @@ console.log(animals[dog]) #### Answer: B -Object keys are converted to strings. +Object keys are converted to strings. -Since the value of `dog` is an object, `animals[dog]` actually means that we’re creating a new property called `"[object Object]"` equal to the new object. `animals["[object Object]"]` is now equal to `{ emoji: "🐢", name: "Mara"}`. +Since the value of `dog` is an object, `animals[dog]` actually means that we’re creating a new property called `"[object Object]"` equal to the new object. `animals["[object Object]"]` is now equal to `{ emoji: "🐢", name: "Mara"}`. -`cat` is also an object, which means that `animals[cat]` actually means that we’re overwriting the value of `animals["[object Object]"]` with the new cat properties. +`cat` is also an object, which means that `animals[cat]` actually means that we’re overwriting the value of `animals["[object Object]"]` with the new cat properties. Logging `animals[dog]`, or actually `animals["[object Object]"]` since converting the `dog` object to a string results `"[object Object]"`, returns the `{ emoji: "🐈", name: "Sara" }`. @@ -4992,14 +4989,14 @@ Logging `animals[dog]`, or actually `animals["[object Object]"]` since convertin ```javascript const user = { - email: "my@email.com", - updateEmail: email => { - this.email = email - } -} + email: "my@email.com", + updateEmail: (email) => { + this.email = email; + }, +}; -user.updateEmail("new@email.com") -console.log(user.email) +user.updateEmail("new@email.com"); +console.log(user.email); ``` - A: `my@email.com` @@ -5012,7 +5009,7 @@ console.log(user.email) #### Answer: A -The `updateEmail` function is an arrow function, and is not bound to the `user` object. This means that the `this` keyword is not referring to the `user` object, but refers to the global scope in this case. The value of `email` within the `user` object does not get updated. When logging the value of `user.email`, the original value of `my@email.com` gets returned. +The `updateEmail` function is an arrow function, and is not bound to the `user` object. This means that the `this` keyword is not referring to the `user` object, but refers to the global scope in this case. The value of `email` within the `user` object does not get updated. When logging the value of `user.email`, the original value of `my@email.com` gets returned.

@@ -5022,20 +5019,20 @@ The `updateEmail` function is an arrow function, and is not bound to the `user` ###### 152. What's the output? ```javascript -const promise1 = Promise.resolve('First') -const promise2 = Promise.resolve('Second') -const promise3 = Promise.reject('Third') -const promise4 = Promise.resolve('Fourth') +const promise1 = Promise.resolve("First"); +const promise2 = Promise.resolve("Second"); +const promise3 = Promise.reject("Third"); +const promise4 = Promise.resolve("Fourth"); const runPromises = async () => { - const res1 = await Promise.all([promise1, promise2]) - const res2 = await Promise.all([promise3, promise4]) - return [res1, res2] -} + const res1 = await Promise.all([promise1, promise2]); + const res2 = await Promise.all([promise3, promise4]); + return [res1, res2]; +}; runPromises() - .then(res => console.log(res)) - .catch(err => console.log(err)) + .then((res) => console.log(res)) + .catch((err) => console.log(err)); ``` - A: `[['First', 'Second'], ['Fourth']]` @@ -5048,23 +5045,26 @@ runPromises() #### Answer: D -The `Promise.all` method runs the passed promises in parallel. If one promise fails, the `Promise.all` method _rejects_ with the value of the rejected promise. In this case, `promise3` is rejected with the value `"Third"`. We’re catching the rejected value in the chained `catch` method on the `runPromises` invocation to catch any errors within the `runPromises` function. Only `"Third"` gets logged, since `promise3` is rejected with this value. +The `Promise.all` method runs the passed promises in parallel. If one promise fails, the `Promise.all` method _rejects_ with the value of the rejected promise. In this case, `promise3` is rejected with the value `"Third"`. We’re catching the rejected value in the chained `catch` method on the `runPromises` invocation to catch any errors within the `runPromises` function. Only `"Third"` gets logged, since `promise3` is rejected with this value.

--- -###### 153. What should the value of `method` be to log `{ name: "Lydia", age: 22 }`? +###### 153. What should the value of `method` be to log `{ name: "Lydia", age: 22 }`? ```javascript -const keys = ["name", "age"] -const values = ["Lydia", 22] +const keys = ["name", "age"]; +const values = ["Lydia", 22]; -const method = /* ?? */ -Object[method](keys.map((_, i) => { - return [keys[i], values[i]] -})) // { name: "Lydia", age: 22 } +const method = + /* ?? */ + Object[method]( + keys.map((_, i) => { + return [keys[i], values[i]]; + }) + ); // { name: "Lydia", age: 22 } ``` - A: `entries` @@ -5077,7 +5077,7 @@ Object[method](keys.map((_, i) => { #### Answer: C -The `fromEntries` method turns a 2d array into an object. The first element in each subarray will be the key, and the second element in each subarray will be the value. In this case, we’re mapping over the `keys` array, which returns an array that the first element is the item on the key array on the current index, and the second element is the item of the values array on the current index. +The `fromEntries` method turns a 2d array into an object. The first element in each subarray will be the key, and the second element in each subarray will be the value. In this case, we’re mapping over the `keys` array, which returns an array that the first element is the item on the key array on the current index, and the second element is the item of the values array on the current index. This creates an array of subarrays containing the correct keys and values, which results in `{ name: "Lydia", age: 22 }` @@ -5089,18 +5089,18 @@ This creates an array of subarrays containing the correct keys and values, which ###### 154. What's the output? ```javascript -const createMember = ({ email, address = {}}) => { - const validEmail = /.+\@.+\..+/.test(email) - if (!validEmail) throw new Error("Valid email pls") +const createMember = ({ email, address = {} }) => { + const validEmail = /.+\@.+\..+/.test(email); + if (!validEmail) throw new Error("Valid email pls"); - return { - email, - address: address ? address : null - } -} + return { + email, + address: address ? address : null, + }; +}; -const member = createMember({ email: "my@email.com" }) -console.log(member) +const member = createMember({ email: "my@email.com" }); +console.log(member); ``` - A: `{ email: "my@email.com", address: null }` @@ -5123,13 +5123,13 @@ The default value of `address` is an empty object `{}`. When we set the variable ###### 155. What's the output? ```javascript -let randomValue = { name: "Lydia" } -randomValue = 23 +let randomValue = { name: "Lydia" }; +randomValue = 23; if (!typeof randomValue === "string") { - console.log("It's not a string!") + console.log("It's not a string!"); } else { - console.log("Yay it's a string!") + console.log("Yay it's a string!"); } ```