DevScrolls
50. What's the output?
[1, 2, 3].map(num => { if (typeof num === 'number') return; return num * 2; });
[1, 2, 3].map(num => { if (typeof num === 'number') return; return num * 2; });
Answer: C
When mapping over the array, the value of num is equal to the element it’s currently looping over. In this case, the elements are numbers, so the condition of the if statement typeof num === "number" returns true. The map function creates a new array and inserts the values returned from the function.
However, we don’t return a value. When we don’t return a value from the function, the function returns undefined. For every element in the array, the function block gets called, so for each element we return undefined.
When mapping over the array, the value of num is equal to the element it’s currently looping over. In this case, the elements are numbers, so the condition of the if statement typeof num === "number" returns true. The map function creates a new array and inserts the values returned from the function.
However, we don’t return a value. When we don’t return a value from the function, the function returns undefined. For every element in the array, the function block gets called, so for each element we return undefined.
51. What's the output?
function getInfo(member, year) {
member.name = 'Lydia';
year = '1998';
}
const person = {
name: 'Sarah'
};
const birthYear = '1997';
getInfo(person, birthYear);
console.log(person, birthYear);
DevScrolls
51. What's the output? function getInfo(member, year) { member.name = 'Lydia'; year = '1998'; } const person = { name: 'Sarah' }; const birthYear = '1997'; getInfo(person, birthYear); console.log(person, birthYear);
DevScrolls
Output?
Answer: A
Arguments are passed by value, unless their value is an object, then they're passed by reference. birthYear is passed by value, since it's a string, not an object. When we pass arguments by value, a copy of that value is created (see question 46).
The variable birthYear has a reference to the value "1997". The argument year also has a reference to the value "1997", but it's not the same value as birthYear has a reference to. When we update the value of year by setting year equal to "1998", we are only updating the value of year. birthYear is still equal to "1997".
The value of person is an object. The argument member has a (copied) reference to the same object. When we modify a property of the object member has a reference to, the value of person will also be modified, since they both have a reference to the same object. person's name property is now equal to the value "Lydia"
Arguments are passed by value, unless their value is an object, then they're passed by reference. birthYear is passed by value, since it's a string, not an object. When we pass arguments by value, a copy of that value is created (see question 46).
The variable birthYear has a reference to the value "1997". The argument year also has a reference to the value "1997", but it's not the same value as birthYear has a reference to. When we update the value of year by setting year equal to "1998", we are only updating the value of year. birthYear is still equal to "1997".
The value of person is an object. The argument member has a (copied) reference to the same object. When we modify a property of the object member has a reference to, the value of person will also be modified, since they both have a reference to the same object. person's name property is now equal to the value "Lydia"
52. What's the output?
function greeting() {
throw 'Hello world!';
}
function sayHi() {
try {
const data = greeting(); console.log('It worked!', data);
} catch (e) {
console.log('Oh no an error:', e);
}
}
sayHi();
DevScrolls
52. What's the output? function greeting() { throw 'Hello world!'; } function sayHi() { try { const data = greeting(); console.log('It worked!', data); } catch (e) { console.log('Oh no an error:', e); } } sayHi();
DevScrolls
52. What's the output? function greeting() { throw 'Hello world!'; } function sayHi() { try { const data = greeting(); console.log('It worked!', data); } catch (e) { console.log('Oh no an error:', e); } } sayHi();
Answer: D
With the throw statement, we can create custom errors. With this statement, you can throw exceptions. An exception can be a string, a number, a boolean or an object. In this case, our exception is the string 'Hello world!'.
With the catch statement, we can specify what to do if an exception is thrown in the try block. An exception is thrown: the string 'Hello world!'. e is now equal to that string, which we log. This results in 'Oh an error: Hello world!'.
With the throw statement, we can create custom errors. With this statement, you can throw exceptions. An exception can be a string, a number, a boolean or an object. In this case, our exception is the string 'Hello world!'.
With the catch statement, we can specify what to do if an exception is thrown in the try block. An exception is thrown: the string 'Hello world!'. e is now equal to that string, which we log. This results in 'Oh an error: Hello world!'.
53. What's the output?
A: "Lamborghini"
B: "Maserati"
C: ReferenceError
D: TypeError
function Car() {
this.make = 'Lamborghini'; return {
make: 'Maserati'
};
}
const myCar = new Car(); console.log(myCar.make);A: "Lamborghini"
B: "Maserati"
C: ReferenceError
D: TypeError
https://thedailyfrontend.com/how-to-think-like-a-frontend-developer/
How to think like a Frontend Developer
How to think like a Frontend Developer
The Daily Frontend
How to think like a Frontend Developer - The Daily Frontend
To think like a frontend developer, you should focus on the following areas are User experience, Browser compatibility, Accessibility etc.
DevScrolls
https://thedailyfrontend.com/how-to-think-like-a-frontend-developer/ How to think like a Frontend Developer
This article is written with the help of Chat GPT 😃 Read this article and it it's helpful then plz comment to improve and publish more articles.✌️
👍1
DevScrolls
Output?
Answer: B
When you return a property, the value of the property is equal to the returned value, not the value set in the constructor function. We return the string "Maserati", so myCar.make is equal to "Maserati".
When you return a property, the value of the property is equal to the returned value, not the value set in the constructor function. We return the string "Maserati", so myCar.make is equal to "Maserati".
54. What's the output?
(() => { let x = (y = 10); })(); console.log(typeof x); console.log(typeof y);
(() => { let x = (y = 10); })(); console.log(typeof x); console.log(typeof y);
Anonymous Poll
71%
A: "undefined", "number"
21%
B: "number", "number"
0%
C: "object", "number"
7%
D: "number", "undefined"
DevScrolls
54. What's the output?
(() => { let x = (y = 10); })(); console.log(typeof x); console.log(typeof y);
(() => { let x = (y = 10); })(); console.log(typeof x); console.log(typeof y);
Answer: A
let x = (y = 10); is actually shorthand for:
When we set y equal to 10, we actually add a property y to the global object (window in browser, global in Node). In a browser, window.y is now equal to 10.
Then, we declare a variable x with the value of y, which is 10. Variables declared with the let keyword are block scoped, they are only defined within the block they're declared in; the immediately invoked function expression (IIFE) in this case. When we use the typeof operator, the operand x is not defined: we are trying to access x outside of the block it's declared in. This means that x is not defined. Values who haven't been assigned a value or declared are of type "undefined". console.log(typeof x) returns "undefined".
However, we created a global variable y when setting y equal to 10. This value is accessible anywhere in our code. y is defined, and holds a value of type "number". console.log(typeof y) returns "number".
let x = (y = 10); is actually shorthand for:
y = 10; let x = y;When we set y equal to 10, we actually add a property y to the global object (window in browser, global in Node). In a browser, window.y is now equal to 10.
Then, we declare a variable x with the value of y, which is 10. Variables declared with the let keyword are block scoped, they are only defined within the block they're declared in; the immediately invoked function expression (IIFE) in this case. When we use the typeof operator, the operand x is not defined: we are trying to access x outside of the block it's declared in. This means that x is not defined. Values who haven't been assigned a value or declared are of type "undefined". console.log(typeof x) returns "undefined".
However, we created a global variable y when setting y equal to 10. This value is accessible anywhere in our code. y is defined, and holds a value of type "number". console.log(typeof y) returns "number".
55. What's the output?
};
const pet = new Dog('Mara');
pet.bark();
delete Dog.prototype.bark; pet.bark();
class Dog {
constructor(name) {
this.name = name;
}
}
Dog.prototype.bark = function() {
console.log(Woof I am ${this.name});};
const pet = new Dog('Mara');
pet.bark();
delete Dog.prototype.bark; pet.bark();
DevScrolls
55. What's the output? class Dog { constructor(name) { this.name = name; } } Dog.prototype.bark = function() { console.log(Woof I am ${this.name}); }; const pet = new Dog('Mara'); pet.bark(); delete Dog.prototype.bark; pet.bark();
DevScrolls
Output?
Answer: A
We can delete properties from objects using the delete keyword, also on the prototype. By deleting a property on the prototype, it is not available anymore in the prototype chain. In this case, the bark function is not available anymore on the prototype after delete Dog.prototype.bark, yet we still try to access it.
When we try to invoke something that is not a function, a TypeError is thrown. In this case TypeError: pet.bark is not a function, since pet.bark is undefined.
We can delete properties from objects using the delete keyword, also on the prototype. By deleting a property on the prototype, it is not available anymore in the prototype chain. In this case, the bark function is not available anymore on the prototype after delete Dog.prototype.bark, yet we still try to access it.
When we try to invoke something that is not a function, a TypeError is thrown. In this case TypeError: pet.bark is not a function, since pet.bark is undefined.
56. What's the output?
const set = new Set([1, 1, 2, 3, 4]); console.log(set);
const set = new Set([1, 1, 2, 3, 4]); console.log(set);
Anonymous Poll
38%
A: [1, 1, 2, 3, 4]
38%
B: [1, 2, 3, 4]
15%
C: {1, 1, 2, 3, 4}
8%
D: {1, 2, 3, 4}
DevScrolls
56. What's the output?
const set = new Set([1, 1, 2, 3, 4]); console.log(set);
const set = new Set([1, 1, 2, 3, 4]); console.log(set);
Answer: D
The Set object is a collection of unique values: a value can only occur once in a set.
We passed the iterable [1, 1, 2, 3, 4] with a duplicate value 1. Since we cannot have two of the same values in a set, one of them is removed. This results in {1, 2, 3, 4}.
The Set object is a collection of unique values: a value can only occur once in a set.
We passed the iterable [1, 1, 2, 3, 4] with a duplicate value 1. Since we cannot have two of the same values in a set, one of them is removed. This results in {1, 2, 3, 4}.
57. What's the output?
// counter.js
let counter = 10;
export default counter;// index.js
import myCounter from './counter';
myCounter += 1; console.log(myCounter);