75. What's the output?
const box = { x: 10, y: 20 }; Object.freeze(box); const shape = box; shape.x = 100; console.log(shape);
const box = { x: 10, y: 20 }; Object.freeze(box); const shape = box; shape.x = 100; console.log(shape);
Anonymous Poll
41%
A: { x: 100, y: 20 }
31%
B: { x: 10, y: 20 }
19%
C: { x: 100 }
9%
D: ReferenceError
DevScrolls
75. What's the output?
const box = { x: 10, y: 20 }; Object.freeze(box); const shape = box; shape.x = 100; console.log(shape);
const box = { x: 10, y: 20 }; Object.freeze(box); const shape = box; shape.x = 100; console.log(shape);
Answer: B
Object.freeze makes it impossible to add, remove, or modify properties of an object (unless the property's value is another object).
When we create the variable shape and set it equal to the frozen object box, shape also refers to a frozen object. You can check whether an object is frozen by using Object.isFrozen. In this case, Object.isFrozen(shape) would return true, since the variable shape has a reference to a frozen object.
Since shape is frozen, and since the value of x is not an object, we cannot modify the property x. x is still equal to 10, and { x: 10, y: 20 } gets logged.
Object.freeze makes it impossible to add, remove, or modify properties of an object (unless the property's value is another object).
When we create the variable shape and set it equal to the frozen object box, shape also refers to a frozen object. You can check whether an object is frozen by using Object.isFrozen. In this case, Object.isFrozen(shape) would return true, since the variable shape has a reference to a frozen object.
Since shape is frozen, and since the value of x is not an object, we cannot modify the property x. x is still equal to 10, and { x: 10, y: 20 } gets logged.
76. What's the output?
const { firstName: myName } = { firstName: 'Lydia' }; console.log(firstName);
const { firstName: myName } = { firstName: 'Lydia' }; console.log(firstName);
Anonymous Poll
43%
A: "Lydia"
29%
B: "myName"
14%
C: undefined
14%
D: ReferenceError
DevScrolls
76. What's the output?
const { firstName: myName } = { firstName: 'Lydia' }; console.log(firstName);
const { firstName: myName } = { firstName: 'Lydia' }; console.log(firstName);
Answer: D
By using destructuring assignment syntax we can unpack values from arrays, or properties from objects, into distinct variables:
Also, a property can be unpacked from an object and assigned to a variable with a different name than the object property:
Therefore, firstName does not exist as a variable, thus attempting to access its value will raise a ReferenceError.
Note: Be aware of the global scope properties:
Whenever Javascript is unable to find a variable within the current scope, it climbs up the Scope chain 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.
In Browsers such as Chrome, name is a deprecated global scope property. In this example, the code is running inside global scope and there is no user defined local variable for name, therefore it searches the predefined variables/properties in the global scope which is in case of browsers, it searches through window object and it will extract the window.name value which is equal to an empty string.
In NodeJS, there is no such property on the global object, thus attempting to access a non-existent variable will raise a ReferenceError.
By using destructuring assignment syntax we can unpack values from arrays, or properties from objects, into distinct variables:
const { firstName } = { firstName: 'Lydia' }; // ES5 version: // var firstName = { firstName: 'Lydia' }.firstName; 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:
const { firstName: myName } = { firstName: 'Lydia' }; // ES5 version: // var myName = { firstName: 'Lydia' }.firstName; console.log(myName); // "Lydia" console.log(firstName); // Uncaught ReferenceError: firstName is not definedTherefore, firstName does not exist as a variable, thus attempting to access its value will raise a ReferenceError.
Note: Be aware of the global scope properties:
const { name: myName } = { name: 'Lydia' }; console.log(myName); // "lydia" console.log(name); // "" ----- Browser e.g. Chrome console.log(name); // ReferenceError: name is not defined ----- NodeJSWhenever Javascript is unable to find a variable within the current scope, it climbs up the Scope chain 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.
In Browsers such as Chrome, name is a deprecated global scope property. In this example, the code is running inside global scope and there is no user defined local variable for name, therefore it searches the predefined variables/properties in the global scope which is in case of browsers, it searches through window object and it will extract the window.name value which is equal to an empty string.
In NodeJS, there is no such property on the global object, thus attempting to access a non-existent variable will raise a ReferenceError.
MDN Web Docs
Destructuring - JavaScript | MDN
The destructuring syntax is a JavaScript syntax that makes it possible to unpack values from arrays, or properties from objects, into distinct variables. It can be used in locations that receive data (such as the left-hand side of an assignment or anywhere…
77. Is this a pure function?
function sum(a, b) { return a + b; }
function sum(a, b) { return a + b; }
Anonymous Poll
78%
A: Yes
22%
B: No
DevScrolls
77. Is this a pure function?
function sum(a, b) { return a + b; }
function sum(a, b) { return a + b; }
Answer: A
A pure function is a function that always returns the same result, if the same arguments are passed.
The sum function always returns the same result. If we pass 1 and 2, it will always return 3 without side effects. If we pass 5 and 10, it will always return 15, and so on. This is the definition of a pure function.
A pure function is a function that always returns the same result, if the same arguments are passed.
The sum function always returns the same result. If we pass 1 and 2, it will always return 3 without side effects. If we pass 5 and 10, it will always return 15, and so on. This is the definition of a pure function.
80. What is the output?
const list = [1 + 2, 1 * 2, 1 / 2]; console.log(list);
const list = [1 + 2, 1 * 2, 1 / 2]; console.log(list);
Anonymous Poll
14%
A: ["1 + 2", "1 * 2", "1 / 2"]
14%
B: ["12", 2, 0.5]
64%
C: [3, 2, 0.5]
7%
D: [1, 1, 1]
DevScrolls
80. What is the output?
const list = [1 + 2, 1 * 2, 1 / 2]; console.log(list);
const list = [1 + 2, 1 * 2, 1 / 2]; console.log(list);
Answer: C
Array elements can hold any value. Numbers, strings, objects, other arrays, null, boolean values, undefined, and other expressions such as dates, functions, and calculations.
The element will be equal to the returned value. 1 + 2 returns 3, 1 * 2 returns 2, and 1 / 2 returns 0.5.
Array elements can hold any value. Numbers, strings, objects, other arrays, null, boolean values, undefined, and other expressions such as dates, functions, and calculations.
The element will be equal to the returned value. 1 + 2 returns 3, 1 * 2 returns 2, and 1 / 2 returns 0.5.
81. What is the output?
function sayHi(name) { return `Hi there, ${name}`; } console.log(sayHi());
function sayHi(name) { return `Hi there, ${name}`; } console.log(sayHi());
Anonymous Poll
33%
A: Hi there,
38%
B: Hi there, undefined
13%
C: Hi there, null
17%
D: ReferenceError
83. What is the output?
const person = { name: 'Lydia', age: 21, }; let city = person.city; city = 'Amsterdam'; console.log(person);
const person = { name: 'Lydia', age: 21, }; let city = person.city; city = 'Amsterdam'; console.log(person);
Anonymous Poll
29%
A: { name: "Lydia", age: 21 }
33%
B: { name: "Lydia", age: 21, city: "Amsterdam" }
21%
C: { name: "Lydia", age: 21, city: undefined }
17%
D: "Amsterdam"
DevScrolls
81. What is the output?
function sayHi(name) { return `Hi there, ${name}`; } console.log(sayHi());
function sayHi(name) { return `Hi there, ${name}`; } console.log(sayHi());
Answer: B
By default, arguments have the value of undefined, unless a value has been passed to the function. In this case, we didn't pass a value for the name argument. name is equal to undefined which gets logged.
In ES6, we can overwrite this default undefined value with default parameters. For example:
function sayHi(name = "Lydia") { ... }
In this case, if we didn't pass a value or if we passed undefined, name would always be equal to the string Lydia
By default, arguments have the value of undefined, unless a value has been passed to the function. In this case, we didn't pass a value for the name argument. name is equal to undefined which gets logged.
In ES6, we can overwrite this default undefined value with default parameters. For example:
function sayHi(name = "Lydia") { ... }
In this case, if we didn't pass a value or if we passed undefined, name would always be equal to the string Lydia
DevScrolls
83. What is the output?
const person = { name: 'Lydia', age: 21, }; let city = person.city; city = 'Amsterdam'; console.log(person);
const person = { name: 'Lydia', age: 21, }; let city = person.city; city = 'Amsterdam'; console.log(person);
Answer: A
We set the variable city equal to the value of the property called city on the person object. There is no property on this object called city, so the variable city has the value of undefined.
Note that we are not referencing the person object itself! We simply set the variable city equal to the current value of the city property on the person object.
Then, we set city equal to the string "Amsterdam". This doesn't change the person object: there is no reference to that object.
When logging the person object, the unmodified object gets returned.
We set the variable city equal to the value of the property called city on the person object. There is no property on this object called city, so the variable city has the value of undefined.
Note that we are not referencing the person object itself! We simply set the variable city equal to the current value of the city property on the person object.
Then, we set city equal to the string "Amsterdam". This doesn't change the person object: there is no reference to that object.
When logging the person object, the unmodified object gets returned.
85. What kind of information would get logged?
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));
Anonymous Poll
53%
A: The result of the fetch method.
26%
B: The result of the second invocation of the fetch method.
5%
C: The result of the callback in the previous .then()
16%
D: It would always be undefined.
https://thedailyfrontend.com/looping-statement-in-javascript/
Hey Folks 😃... I am about to write frontend articles every week from the basic concepts in JavaScript to Advance concepts with detailed explanations with real world scenarios for where to use and how to use them. Kindly give your support and happy learning 🤗
Hey Folks 😃... I am about to write frontend articles every week from the basic concepts in JavaScript to Advance concepts with detailed explanations with real world scenarios for where to use and how to use them. Kindly give your support and happy learning 🤗
The Daily Frontend
Looping Statement in JavaScript - The Daily Frontend
Looping statement in JavaScript allows us to run the same set of code again and again until some conditions are met. let's see in detail.
DevScrolls
85. What kind of information would get logged?
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));
Answer: C
The value of res in the second .then is equal to the returned value of the previous .then. You can keep chaining .thens like this, where the value is passed to the next handler.
The value of res in the second .then is equal to the returned value of the previous .then. You can keep chaining .thens like this, where the value is passed to the next handler.
86. Which option is a way to set hasName equal to true, provided you cannot pass true as an argument?
function getName(name) { const hasName = // }
function getName(name) { const hasName = // }
Anonymous Poll
35%
A: !!name
26%
B: name
30%
C: new Boolean(name)
9%
D: name.length
DevScrolls
86. Which option is a way to set hasName equal to true, provided you cannot pass true as an argument?
function getName(name) { const hasName = // }
function getName(name) { const hasName = // }
Answer: A
With !!name, we determine whether the value of name is truthy or falsy. If name is truthy, which we want to test for, !name returns false. !false (which is what !!name practically is) returns true.
By setting hasName equal to name, you set hasName equal to whatever value you passed to the getName function, not the boolean value true.
new Boolean(true) returns an object wrapper, not the boolean value itself.
name.length returns the length of the passed argument, not whether it's true.
With !!name, we determine whether the value of name is truthy or falsy. If name is truthy, which we want to test for, !name returns false. !false (which is what !!name practically is) returns true.
By setting hasName equal to name, you set hasName equal to whatever value you passed to the getName function, not the boolean value true.
new Boolean(true) returns an object wrapper, not the boolean value itself.
name.length returns the length of the passed argument, not whether it's true.
87. What's the output?
console.log('I want pizza'[0]);
console.log('I want pizza'[0]);
Anonymous Poll
17%
A: """
25%
B: "I"
17%
C: SyntaxError
42%
D: undefined
DevScrolls
87. What's the output?
console.log('I want pizza'[0]);
console.log('I want pizza'[0]);
Answer: B
In order to get a character at a specific index of a string, you can use bracket notation. The first character in the string has index 0, and so on. In this case, we want to get the element with index 0, the character "I', which gets logged.
Note that this method is not supported in IE7 and below. In that case, use .charAt().
In order to get a character at a specific index of a string, you can use bracket notation. The first character in the string has index 0, and so on. In this case, we want to get the element with index 0, the character "I', which gets logged.
Note that this method is not supported in IE7 and below. In that case, use .charAt().
88. What's the output?
function sum(num1, num2 = num1) { console.log(num1 + num2); } sum(10);
function sum(num1, num2 = num1) { console.log(num1 + num2); } sum(10);
Anonymous Poll
33%
A: NaN
28%
B: 20
28%
C: ReferenceError
11%
D: undefined
DevScrolls
88. What's the output?
function sum(num1, num2 = num1) { console.log(num1 + num2); } sum(10);
function sum(num1, num2 = num1) { console.log(num1 + num2); } sum(10);
Answer: B
You can set a default parameter's value equal to another parameter of the function, as long as they've been defined before the default parameter. We pass the value 10 to the sum function. If the sum function only receives 1 argument, it means that the value for num2 is not passed, and the value of num1 is equal to the passed value 10 in this case. The default value of num2 is the value of num1, which is 10. num1 + num2 returns 20.
If you're trying to set a default parameter's value equal to a parameter which is defined after (to the right), the parameter's value hasn't been initialized yet, which will throw an error.
You can set a default parameter's value equal to another parameter of the function, as long as they've been defined before the default parameter. We pass the value 10 to the sum function. If the sum function only receives 1 argument, it means that the value for num2 is not passed, and the value of num1 is equal to the passed value 10 in this case. The default value of num2 is the value of num1, which is 10. num1 + num2 returns 20.
If you're trying to set a default parameter's value equal to a parameter which is defined after (to the right), the parameter's value hasn't been initialized yet, which will throw an error.