70. What's the output?
console.log('π₯' + 'π»');
console.log('π₯' + 'π»');
Anonymous Poll
65%
A: "π₯π»"
0%
B: 257548
14%
C: A string containing their code points
22%
D: Error
DevScrolls
69. What's the output?
const name = 'Lydia Hallie'; console.log(name.padStart(13)); console.log(name.padStart(2));
const name = 'Lydia Hallie'; console.log(name.padStart(13)); console.log(name.padStart(2));
Answer: C
With the padStart method, we can add padding to the beginning of a string. The value passed to this method is the total length of the string together with the padding. The string "Lydia Hallie" has a length of 12. name.padStart(13) inserts 1 space at the start of the string, because 12 + 1 is 13.
If the argument passed to the padStart method is smaller than the length of the array, no padding will be added.
With the padStart method, we can add padding to the beginning of a string. The value passed to this method is the total length of the string together with the padding. The string "Lydia Hallie" has a length of 12. name.padStart(13) inserts 1 space at the start of the string, because 12 + 1 is 13.
If the argument passed to the padStart method is smaller than the length of the array, no padding will be added.
DevScrolls
70. What's the output?
console.log('π₯' + 'π»');
console.log('π₯' + 'π»');
Answer: A
With the + operator, you can concatenate strings. In this case, we are concatenating the string "π₯" with the string "π»", resulting in "π₯π»".
With the + operator, you can concatenate strings. In this case, we are concatenating the string "π₯" with the string "π»", resulting in "π₯π»".
72. What's the output?
console.log(String.raw`Hello\nworld`);
console.log(String.raw`Hello\nworld`);
Anonymous Poll
38%
A: Hello world!
16%
B: Hello world
41%
C: Hello\nworld
6%
D: Hello\n world
DevScrolls
72. What's the output?
console.log(String.raw`Hello\nworld`);
console.log(String.raw`Hello\nworld`);
Answer: C
String.raw returns a string where the escapes (\n, \v, \t etc.) are ignored! Backslashes can be an issue since you could end up with something like:
const path =
Which would result in:
"C:DocumentsProjects able.html"
With String.raw, it would simply ignore the escape and print:
C:\Documents\Projects\table.html
In this case, the string is Hello\nworld, which gets logged.
String.raw returns a string where the escapes (\n, \v, \t etc.) are ignored! Backslashes can be an issue since you could end up with something like:
const path =
C:\Documents\Projects\table.htmlWhich would result in:
"C:DocumentsProjects able.html"
With String.raw, it would simply ignore the escape and print:
C:\Documents\Projects\table.html
In this case, the string is Hello\nworld, which gets logged.
73. What's the output?
async function getData() { return await Promise.resolve('I made it!'); } const data = getData(); console.log(data);
async function getData() { return await Promise.resolve('I made it!'); } const data = getData(); console.log(data);
Anonymous Poll
64%
A: "I made it!"
9%
B: Promise {<resolved>: "I made it!"}
9%
C: Promise {<pending>}
18%
D: undefined
DevScrolls
73. What's the output?
async function getData() { return await Promise.resolve('I made it!'); } const data = getData(); console.log(data);
async function getData() { return await Promise.resolve('I made it!'); } const data = getData(); console.log(data);
Answer: C
An async function always returns a promise. The await still has to wait for the promise to resolve: a pending promise gets returned when we call getData() in order to set data equal to it.
If we wanted to get access to the resolved value "I made it", we could have used the .then() method on data:
data.then(res => console.log(res))
This would've logged "I made it!"
An async function always returns a promise. The await still has to wait for the promise to resolve: a pending promise gets returned when we call getData() in order to set data equal to it.
If we wanted to get access to the resolved value "I made it", we could have used the .then() method on data:
data.then(res => console.log(res))
This would've logged "I made it!"
74. What's the output?
function addToList(item, list) {
return list.push(item);
}
const result = addToList('apple', ['banana']);
console.log(result);
DevScrolls
Output ?
Answer: B
The .push() method returns the length of the new array! Previously, the array contained one element (the string "banana") and had a length of 1. After adding the string "apple" to the array, the array contains two elements, and has a length of 2. This gets returned from the addToList function.
The push method modifies the original array. If you wanted to return the array from the function rather than the length of the array, you should have returned list after pushing item to it.
The .push() method returns the length of the new array! Previously, the array contained one element (the string "banana") and had a length of 1. After adding the string "apple" to the array, the array contains two elements, and has a length of 2. This gets returned from the addToList function.
The push method modifies the original array. If you wanted to return the array from the function rather than the length of the array, you should have returned list after pushing item to it.
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