DevScrolls
59 subscribers
2 photos
8 files
86 links
A thoughtful builder's journal β€” exploring modern frontend architecture, state management, design systems, and the craft of resilient software.

For more:

https://www.devscrolls.dev
Download Telegram
DevScrolls
36. What's the output?

console.log(typeof typeof 1);
Answer: B
typeof 1 returns "number". typeof "number" returns "string"
πŸ‘1
37. What's the output?

const numbers = [1, 2, 3]; numbers[10] = 11; console.log(numbers);
Anonymous Poll
29%
A: [1, 2, 3, null x 7, 11]
21%
B: [1, 2, 3, 11]
29%
C: [1, 2, 3, empty x 7, 11]
21%
D: SyntaxError
DevScrolls
37. What's the output?

const numbers = [1, 2, 3]; numbers[10] = 11; console.log(numbers);
Answer: C
When you set a value to an element in an array that exceeds the length of the array, JavaScript creates something called "empty slots". These actually have the value of undefined, but you will see something like:
[1, 2, 3, empty x 7, 11]
depending on where you run it (it's different for every browser, node, etc.)
38. What's the output?

(() => {
let x, y;
try {
throw new Error();
} catch (x) {
(x = 1), (y = 2);
console.log(x);
}
console.log(x); console.log(y);
})();


A: 1 undefined 2
B: undefined undefined undefined
C: 1 1 2
D: 1 undefined undefined
DevScrolls
38. What's the output? (() => { let x, y; try { throw new Error(); } catch (x) { (x = 1), (y = 2); console.log(x); } console.log(x); console.log(y); })(); A: 1 undefined 2 B: undefined undefined undefined C: 1 1 2 D: 1 undefined undefined
Answer: A
The catch block receives the argument x. This is not the same x as the variable when we pass arguments. This variable x is block-scoped.
Later, we set this block-scoped variable equal to 1, and set the value of the variable y. Now, we log the block-scoped variable x, which is equal to 1.
Outside of the catch block, x is still undefined, and y is 2. When we want to console.log(x) outside of the catch block, it returns undefined, and y returns 2.
DevScrolls
39. Everything in JavaScript is either ....
Answer: A
JavaScript only has primitive types and objects.
Primitive types are boolean, null, undefined, bigint, number, string, and symbol.
What differentiates a primitive from an object is that primitives do not have any properties or methods; however, you'll note that 'foo'.toUpperCase() evaluates to 'FOO' and does not result in a TypeError. This is because when you try to access a property or method on a primitive like a string, JavaScript will implicitly wrap the primitive type using one of the wrapper classes, i.e. String, and then immediately discard the wrapper after the expression evaluates. All primitives except for null and undefined exhibit this behaviour.
40. What's the output?

[[0, 1], [2,3]].reduce( (acc, cur) => {
return acc.concat(cur);
}, [1, 2], );


A: [0, 1, 2, 3, 1, 2]
B: [6, 1, 2]
C: [1, 2, 0, 1, 2, 3]
D: [1, 2, 6]
DevScrolls
40. What's the output? [[0, 1], [2,3]].reduce( (acc, cur) => { return acc.concat(cur); }, [1, 2], ); A: [0, 1, 2, 3, 1, 2] B: [6, 1, 2] C: [1, 2, 0, 1, 2, 3] D: [1, 2, 6]
Answer: C
[1, 2] is our initial value. This is the value we start with, and the value of the very first acc. During the first round, acc is [1,2], and cur is [0, 1]. We concatenate them, which results in [1, 2, 0, 1].
Then, [1, 2, 0, 1] is acc and [2, 3] is cur. We concatenate them, and get [1, 2, 0, 1, 2, 3]
DevScrolls
41. What's the output?

!!null; !!' '; !!1;
Answer: B
null is falsy. !null returns true. !true returns false.
"" is falsy. !"" returns true. !true returns false.
1 is truthy. !1 returns false. !false returns true.
42. What does the setInterval method return in the browser?

setInterval(() => console.log('Hi'), 1000);
Anonymous Poll
57%
A: a unique id
14%
B: the amount of milliseconds specified
29%
C: the passed function
0%
D: undefined
DevScrolls
42. What does the setInterval method return in the browser?

setInterval(() => console.log('Hi'), 1000);
Answer: A
It returns a unique id. This id can be used to clear that interval with the clearInterval() function.
DevScrolls
43. What does this return?

[...'Lydia'];
Answer: A
A string is an iterable. The spread operator maps every character of an iterable to one element.
44. What's the output?

function* generator(i) {
yield i;
yield i * 2;
}

const gen = generator(10); console.log(gen.next().value); console.log(gen.next().value);
DevScrolls
What's output?
Answer: C
Regular functions cannot be stopped mid-way after invocation. However, a generator function can be "stopped" midway, and later continue from where it stopped. Every time a generator function encounters a yield keyword, the function yields the value specified after it. Note that the generator function in that case doesn’t return the value, it yields the value.
First, we initialize the generator function with i equal to 10. We invoke the generator function using the next() method. The first time we invoke the generator function, i is equal to 10. It encounters the first yield keyword: it yields the value of i. The generator is now "paused", and 10 gets logged.
Then, we invoke the function again with the next() method. It starts to continue where it stopped previously, still with i equal to 10. Now, it encounters the next yield keyword, and yields i * 2. i is equal to 10, so it returns 10 * 2, which is 20. This results in 10, 20.