preface

Recently I have been reviewing the JavaScript part, if I stay in the basic knowledge, the harvest may not be very great, so I plan to do a common JS interview questions in my spare time to recharge my batteries ๐Ÿ”‹๐Ÿ”‹

โ

Of course, there must be someone who has done similar interesting things, I think they go to do, sum up, to consolidate their knowledge helps โœ”๏ธ

Your encouraging thumbs-up ๐Ÿ‘ collection is what keeps this article going

โž

The following is my collection of some interesting, easy to error JS code questions, the investigation of JS basic knowledge points are involved, the content may not be comprehensive, I hope to help you.


๐Ÿ‘จ๐Ÿ’ป due to typography plus content (1.8W word ๐Ÿ‘, so we plan to divide into two issues, you can click here to check the next issue)


1. “What is the output of the following code?”

const one = (false| | {} | |null)
const two = (null || false || "")
const three = ([] || 0 || true)

console.log(one, two, three)
Copy the code
  • A: false null []
  • B: null "" true
  • C: {} "" []
  • D: null null true
The answer

Answer: C

Using the | | operator, we can return the first true value. If all values are false, the last value is returned.

(false | | {} | | null) : an empty object {} is a true value. This is the first (and only) truth value that will be returned. One is equal to {}.

(null | | false | | “”) : all values are false. This means returning the passed value “”. Two is equal to “”.

([] | | 0 | | “”) : an empty array [] is a true value. This is the first true value returned. Three equals [].

2. “What does this return?”

[..."Lydia"];
Copy the code
  • A: [“L”, “y”, “d”, “i”, “a”]
  • B: [“Lydia”]
  • C: [[], “Lydia”]
  • D: [[“L”, “y”, “d”, “i”, “a”]]
The answer

Answer: A,

Strings are iterable. The extension operator maps each character of the iteration to an element.

3. “What is the output of the following code?”

[[0.1], [2.3]].reduce(
 (acc, cur) = > {
  return acc.concat(cur);
 },
 [1.2]
); Copy the code
  • A: [0, 1, 2, 3, 1, 2]
  • B: [6, 1, 2]
  • C: [1, 2, 0, 1, 2, 3]
  • D: [1, 2, 6]
The answer

Answer: C

[1,2] is our initial value. This is the initial value from which we start executing the Reduce function, and the value of the first ACC. In the first round, acc is [1,2] and cur is [0,1]. We connect them and the result is [1,2,0,1].

Then, acc has a value of [1,2,0,1] and cur has a value of [2,3]. We connect them to get [1,2,0,1,2,3].

4. “What is the output of the following code?”

((a)= > {
 let x, y;
 try {
  throw new Error(a); } catch (x) {
 (x = 1), (y = 2);  console.log(x);  }  console.log(x);  console.log(y); }) (); Copy the code
  • A: 1 undefined 2
  • B: undefined undefined undefined
  • C: 1 1 2
  • D: 1 undefined undefined
The answer

Answer: A,

The catch block receives parameter X. When we pass parameters, this is different from x for variables. This variable x is in the catch scope.

After that, we set the block-level scope variable to 1 and set the value of variable y. Now, we print 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 console.log(x) outside the catch block, it returns undefined and y returns 2.

5. “What is the output of the following code?”

const numbers = [1.2.3];
numbers[10] = 11;
console.log(numbers);
Copy the code
  • A: [1, 2, 3, 7 x null, 11]
  • B: [1, 2, 3, 11]
  • C: [1, 2, 3, 7 x empty, 11]
  • D: SyntaxError
The answer

Answer: C * * * *

When you set a value for an element in an array that exceeds the array’s length, JavaScript creates something called an “empty slot.” The values for these positions are actually undefined, but you’ll see something similar:

[1, 2, 3, 7 x empty, 11]

This depends on where you are running it (each browser may be different)

6. “What is the output of the following code?”

function sayHi() {
 return (() = > 0) ();}

typeof sayHi();
Copy the code
  • A: “object”
  • B: “number”
  • C: “function”
  • D: “undefined”
The answer

Answer: * * * * B

The sayHi function returns the return value of the immediately called function (IIFE). This function returns 0 and is of type number.

7. “What is the output of the following code?”

const person = { name: "Lydia" };

function sayHi(age) {
 console.log(`The ${this.name} is ${age}`);
}
 sayHi.call(person, 21); sayHi.bind(person, 21); Copy the code
  • A: undefined is 21 Lydia is 21
  • B: function function
  • C: Lydia is 21 Lydia is 21
  • D: Lydia is 21 function
The answer

Answer: D * * * *

Using both, we can pass the object we want the this keyword to reference. However, the.call method executes immediately!

The.bind method returns a copy of the value of the function, but with the binding context! It will not be executed immediately.

8. “What does clicking on the following HTML fragment print?”

<div onclick="console.log('div')">
 <p onclick="console.log('p')">
 Click here!

</div>
Copy the code
  • A: p div
  • B: div p
  • C: p
  • D: div
The answer

Answer: * * * * A

If we click p, we see two logs: p and div. During event propagation, there are three phases: capture, target, and bubble. By default, event handlers execute in the bubble phase (unless you set useCapture to true). It extends outward from the deepest nested element.

9. “What is the output of the following code?”

const foo = (a)= > console.log("First");
const bar = (a)= > setTimeout((a)= > console.log("Second"));
const baz = (a)= > console.log("Third");

bar();
foo(); baz(); Copy the code
  • A: First Second Third
  • B: First Third Second
  • C: Second First Third
  • D: Second Third First
The answer

Answer: * * * * B

We have a setTimeout function and call it first. And yet I printed it.

That’s because in the browser, we don’t just have a run-time engine, we have something called a WebAPI. WebAPI provides us with setTimeout functions, such as DOM.

After pushing the callback to WebAPI, the setTimeout function itself (but not the callback!) Pop off the stack.

10. “What is the output of the following code?”

const a = {};
const b = { key: "b" };
const c = { key: "c" };

a[b] = 123;
a[c] = 456;  console.log(a[b]); Copy the code
  • A: 123
  • B: 456
  • C: undefined
  • D: ReferenceError
The answer

Answer: * * * * B

Object keys are automatically converted to strings. We are trying to set an object to be the key of object A with a value of 123.

However, when the Object is automatically converted to stringification, it becomes Object Object. So here we say a[“Object Object “] = 123. Then, we can try to do the same thing again. C objects are also implicitly cast. So, a[“Object Object “] = 456.

Then, we print a[b], which is actually a[“Object Object “]. We set it to 456, so return 456.

11. “What are the three stages of event transmission?”

  • A: Target > Capturing > Bubbling
  • B: Bubbling > Target > Capturing
  • C: Target > Bubbling > Capturing
  • D: Capturing > Target > Bubbling
The answer

Answer: D

In the “capturing” phase, events propagate down from ancestor elements to target elements. The bubbling begins when the event reaches the Target element.

12. “What is the output of the following code?”

function Person(firstName, lastName) {
  this.firstName = firstName
  this.lastName = lastName
}

const lydia = new Person('Lydia'.'Hallie') const sarah = Person('Sarah'.'Smith')  console.log(lydia) console.log(sarah) Copy the code
  • A: Person {firstName: "Lydia", lastName: "Hallie"} and undefined
  • B: Person {firstName: "Lydia", lastName: "Hallie"} and Person {firstName: "Sarah", lastName: "Smith"}
  • C: Person {firstName: "Lydia", lastName: "Hallie"} and {}
  • D:Person {firstName: "Lydia", lastName: "Hallie"} and ReferenceError
The answer

Answer: A,

For Sarah, we don’t use the new keyword. When using new, this refers to the empty object we created. When new is not used, this refers to a “global object”.

We say this.firstName equals “Sarah” and this.lastName equals “Smith”. What we’ve actually done is define global.firstName = ‘Sarah’ and global.lastName = ‘Smith’. And Sarah herself is undefined.


13. “What is the output of the following code?”

function Person(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

const member = new Person("Lydia"."Hallie"); Person.getFullName = function () {  return `The ${this.firstName} The ${this.lastName}`; }  console.log(member.getFullName()); Copy the code
  • A: TypeError
  • B: SyntaxError
  • C: Lydia Hallie
  • D: undefined undefined
The answer

Answer: A,

You can’t add attributes to a constructor like you would to a regular object. If you want to add features to all instances at once, you should use prototypes. Therefore, in this example, the following method is used:

Person.prototype.getFullName = function () {
  return `The ${this.firstName} The ${this.lastName}`;
}
Copy the code

That’s what makes member.getFullName() work. Why is it good to do that? Suppose we add this method to the constructor itself. This method may not be required for every Person instance. This wastes a lot of memory space because they still have that property, which takes up memory space per instance. Conversely, if we just add it to the prototype, it only exists in one location in memory, but all instances can access it!


14. “All Objects have Prototypes”

  • A: true
  • B: false
The answer

Answer: B

All objects except “base objects” have prototypes. Basic objects have access to some methods and attributes, such as.toString. That’s why you can use built-in JavaScript methods! All of these methods are available on the prototype. Although JavaScript cannot find these methods directly on objects, JavaScript will find them along the prototype chain so that you can use them.

15. “What is the output of the following code?”

function sum(a, b) {
  return a + b
}

sum(1.'2')
Copy the code
  • A: NaN
  • B: TypeError
  • C: "12"
  • D: 3
The answer

Answer: C

JavaScript is a “dynamically typed language” : we don’t specify the types of certain variables. Values can be automatically converted to another type without you knowing it, called implicit Type Coercion. Coercion means to convert one type to another.

In this case, JavaScript converts the number 1 to a string so that the function makes sense and returns a value. When the numeric type (1) is added to the string type (‘2’), the number is treated as a string. We can concatenate strings like “Hello” + “World”, what’s happening here is “1” + “2”, it returns “12”.


16. “What is the output of the following code?”

let number = 0
console.log(number++)
console.log(++number)
console.log(number)
Copy the code
  • A: 1 1 2
  • B: 1 2 2
  • C: 0 2 2
  • D: 0 1 2
The answer

Answer: C

Unary “increment after” operator ++ :

  1. Return value (Return0)
  2. Value increment (number is now1)

Unary “increment before” operator ++ :

  1. Value increment (number is now2)
  2. Return value (Return2)

It’s 0, 2, 2.


17. “What is the output of the following code?”

function getPersonInfo(one, two, three) {
  console.log(one)
  console.log(two)
  console.log(three)
}
 const person = 'Lydia' const age = 21  getPersonInfo`${person} is ${age} years old` Copy the code
  • A: "Lydia" 21 ["", " is ", " years old"]
  • B: ["", " is ", " years old"] "Lydia" 21
  • C: "Lydia" ["", " is ", " years old"] 21
The answer

Answer: B

If a tag template literal is used, the value of the first argument always contains an array of strings. The rest of the arguments take the value of the passed expression!


18. “What is the output of the following code?”

function checkAge(data) {
  if (data === { age: 18{})    console.log('You are an adult! ')
  } else if (data == { age: 18{})    console.log('You are still an adult.')
 } else {  console.log(`Hmm.. You don't have an age I guess`)  } }  checkAge({ age: 18 }) Copy the code
  • A: You are an adult!
  • B: You are still an adult.
  • C: Hmm.. You don't have an age I guess
The answer

Answer: C

When testing for equality, primitive types are compared by their values, while objects are compared by their references. JavaScript checks if an object has a reference to the same location in memory.

The two objects we are comparing are not the same reference: the memory location of the object reference passed as an argument is not the same as the memory location of the object used to determine equality.

This is why {age: 18} === {age: 18} and {age: 18} == {age: 18} both return false.


19. “What is the output of the following code?”

function getAge(. args) {
  console.log(typeof args)
}

getAge(21)
Copy the code
  • A: "number"
  • B: "array"
  • C: "object"
  • D: "NaN"
The answer

Answer: C

Extension operators (… Args) returns an array of arguments. Arrays are objects, so Typeof args returns “object”.


20. “What is the output of the following code?”

function getAge() {
  'use strict'
  age = 21
  console.log(age)
}
 getAge() Copy the code
  • A: 21
  • B: undefined
  • C: ReferenceError
  • D: TypeError
The answer

Answer: C

With “Use strict”, you can ensure that you don’t accidentally declare global variables. We never declared the variable age because we used “use strict”, which would throw a reference error. If we don’t use “use strict”, it will work because the age attribute is added to the global object.


21. “What is the output of the following code?”

const getList = ([x, ...y]) = > [x, y]
const getUser = user= > { name: user.name, age: user.age }

const list = [1.2.3.4]
const user = { name: "Lydia".age: 21 }
 console.log(getList(list)) console.log(getUser(user)) Copy the code
  • A: [1, 2, 3, 4]] and undefined
  • B: [1, 2, 3, 4]] and { name: "Lydia", age: 21 }
  • C: [1, 2, 3, 4] and { name: "Lydia", age: 21 }
  • D: Error and { name: "Lydia", age: 21 }
The answer

Answer: A,

The getList function takes an array as its argument. Between the parentheses of the getList function, we immediately deconstruct the array. You can think of it as:

[x, ...y] = [1, 2, 3, 4]

Using the remaining parameters… Y, we put all the remaining arguments in an array. In this case, the remaining parameters are 2,3 and 4. The value of y is an array containing all the remaining parameters. In this case, the value of x is 1, so when we print [x, y], we print [1, [2,3,4]].

The getUser function receives an object. For arrow functions, if only one value is returned, we do not need to write curly braces. However, if you want to return an object from an arrow function, you must write it between parentheses or nothing will be returned! The following function returns an object:

const getUser = user => ({ name: user.name, age: user.age })

Since no value is returned in this case, the function returns undefined.


22. “What is the output of the following code?”

const info = {
  [Symbol('a')]: 'b'
}

console.log(info)
console.log(Object.keys(info)) Copy the code
  • A: {Symbol('a'): 'b'} and ["{Symbol('a')"]
  • B: {} and []
  • C: { a: "b" } and ["a"]
  • D: {Symbol('a'): 'b'} and []
The answer

Answer: D

The Symbol type is not enumerable. The object.keys method returns all the enumerable key properties on an Object. The Symbol type is invisible and returns an empty array. When recording the entire object, all properties are visible, even non-enumerable properties.

This is one of Symbol’s many features: in addition to representing completely unique values (preventing accidental object name conflicts, such as when using two libraries that want to add attributes to the same object), you can also hide the attributes of objects in this way (although not completely). You can still use the Object. GetOwnPropertySymbols visit Symbol () method.


23. “What is the output of the following code?”

class Person {
  constructor() {
    this.name = "Lydia"
  }
}
 Person = class AnotherPerson {  constructor() {  this.name = "Sarah"  } }  const member = new Person() console.log(member.name) Copy the code
  • A: "Lydia"
  • B: "Sarah"
  • C: Error: cannot redeclare Person
  • D: SyntaxError
The answer

Answer: B

We can set the class to be equal to other class/function constructors. In this case, we set Person to AnotherPerson. The constructor name is Sarah, so the name attribute on the new Person instance member is Sarah.


24. “What is the output of the following code?”

function nums(a, b) {
  if
  (a > b)
  console.log('a is bigger')
  else 
 console.log('b is bigger')  return  a + b }  console.log(nums(4.2)) console.log(nums(1.2)) Copy the code
  • A: a is bigger.6 and b is bigger.3
  • B: a is bigger.undefined and b is bigger.undefined
  • C: undefined and undefined
  • D: SyntaxError
The answer

Answer: B

In JavaScript, we don’t have to explicitly write semicolons (;) , but the JavaScript engine still automatically adds semicolons after statements. This is called automatic semicolon insertion. For example, a statement can be a variable, or a keyword like throw, return, or break.

Here, we write a return statement and another value a + b on a new line. However, since it’s a new line, the engine doesn’t know that it’s actually the value we want to return. Instead, it automatically adds a semicolon after the return. You can look at it this way:

  return;
  a + b
Copy the code

This means that it never reaches a + B because the function stops running after the return keyword. If there is no return value, as here, the function returns undefined. Note that there is no automatic insertion after the if/else statement!


25. “What is the output of the following code?”

function getItems(fruitList, ... args, favoriteFruit) {
  return [...fruitList, ...args, favoriteFruit]
}

getItems(["banana"."apple"]."pear"."orange")
Copy the code
  • A: ["banana", "apple", "pear", "orange"]
  • B: [["banana", "apple"], "pear", "orange"]
  • C: ["banana", "apple", ["pear"], "orange"]
  • D: SyntaxError
The answer

Answer: D

. Args is the remaining parameter, and the value of the remaining parameter is an array containing all remaining parameters, “and only as the last parameter.” In the example above, the remaining argument is the second argument, which is impossible and throws a syntax error.

function getItems(fruitList, favoriteFruit, ... args) {
  return [...fruitList, ...args, favoriteFruit]
}

getItems(["banana"."apple"]."pear"."orange")
Copy the code

The above example is valid and will return arrays: [‘banana’, ‘apple’, ‘orange’, ‘Pear’]


26. “What is the output of the following code?”

const person = {
  name: "Lydia".  age: 21
}

for (const [x, y] of Object.entries(person)) {  console.log(x, y) } Copy the code
  • A: name Lydia and age 21
  • B: ["name", "Lydia"] and ["age", 21]
  • C: ["name", "age"] and undefined
  • D: Error
The answer

Answer: A,

The object.entries () method returns an array of key-value pairs for a given Object’s own enumerable properties. In this case, it returns a two-dimensional array where each element is an array of keys and values:

[['name', 'Lydia'], ['age', 21]

With a for-of loop, we can iterate over each element in an array, in this case subarrays. We can use const [x, y] to deconstruct subarrays in a for-of loop. X is equal to the first element in the subarray, and y is equal to the second element in the subarray.

The first subarray is [” name “, “Lydia”], where X equals name and Y equals Lydia. The second subarray is [” age “, 21], where x equals age and y equals 21.


27. “What is the output of the following code?”

function giveLydiaPizza() {
  return "Here is pizza!"
}

const giveLydiaChocolate = (a)= > "Here's chocolate... now go hit the gym already."
 console.log(giveLydiaPizza.prototype) console.log(giveLydiaChocolate.prototype) Copy the code
  • A: { constructor: ... } { constructor: ... }
  • B: {} { constructor: ... }
  • C: { constructor: ... } {}
  • D: { constructor: ... } undefined
The answer

Answer: D

Regular functions, such as the giveLydiaPizza function, have a Prototype attribute, which is an object with the constructor attribute. However, arrow functions, such as the giveLydiaChocolate function, do not have this Prototype attribute. Try to use giveLydiaChocolate. Prototype access prototype property when it returns undefined.


28. “What is the output of the following code?”

let newList = [1.2.3].push(4)

console.log(newList.push(5))
Copy the code
  • A: [1, 2, 3, 4, 5]
  • B: [1, 2, 3, 5]
  • C: [1, 2, 3, 4]
  • D: Error
The answer

Answer: D

The.push method returns the length of the array, not the array itself! By setting newList to [1,2,3].push(4), newList is actually equal to the new length of the array: 4.

Then, try using the.push method on newList. Since newList is a value of 4, TypeError is raised.


29. “What is the output of the following code?”

console.log("I want pizza"[0])
Copy the code
  • A: "" "
  • B: "I"
  • C: SyntaxError
  • D: undefined
The answer

Answer: B

You can use square bracket notation to get the characters of a particular index in a string, where the first character has the index 0, and so on. In this case, we want to get the element with an index of 0, and the character ‘I’ is recorded.

Note that this method is not supported by IE7 and earlier. In this case,.charat () should be used


30. “What is the output of the following code?”

function sum(num1, num2 = num1) {
  console.log(num1 + num2)
}

sum(10)
Copy the code
  • A: NaN
  • B: 20
  • C: ReferenceError
  • D: undefined
The answer

Answer: B

You can set the value of the default parameter to another parameter of the function, as long as the other parameter is defined before it. We pass the value 10 to the sum function. If the sum function takes only one argument, it means that no num2 is passed, in which case num1 is equal to the passed value of 10. The default value for num2 is the value of num1, which is 10. Num1 + num2 returns 20.

If you try to set the value of the default parameter to the parameter defined later, the value of the parameter may not be initialized, resulting in an error. Such as:

function test(m = n, n = 2) {
 console.log(m, n)
}
test() // Uncaught ReferenceError: Cannot access 'n' before initialization
test(3) 2 / / 3
test(3.4) / / 3 4 Copy the code


31. “What is the output of the following code?”

// module.js 
export default() = >"Hello world"
export const name = "Lydia"

// index.js 
import * as data from "./module"  console.log(data) Copy the code
  • A: { default: function default(), name: "Lydia" }
  • B: { default: function default() }
  • C: { default: "Hello world", name: "Lydia" }
  • D: Global object of module.js
The answer

Answer: A,

Using the import * as name syntax, we import all exports from the module.js file into the index.js file and create a new object named data. In the module.js file, there are two exports: default and named. The default export is a function that returns the string “Hello World”, and the named export is a variable named name whose value is the string “Lydia”.

The data object has the default exported property, and the other properties have the name of the specified exports and its corresponding value.


32. “What is the output of the following code?”

class Person {
  constructor(name) {
    this.name = name
  }
}
 const member = new Person("John") console.log(typeof member) Copy the code
  • A: "class"
  • B: "function"
  • C: "object"
  • D: "string"
The answer

Answer: C

Class is the syntactic sugar of the constructor, and if you overwrite the Person class as a constructor it will look like this:

function Person() {
  this.name = name
}
Copy the code

Calling the constructor with new generates an instance of the constructor Person, on which typeof the keyword returns “object”, in which case “object” is printed.


33. “What is the output of the following code?”

const person = { name: 'Lydia' }

function sayHi(age) {
  console.log(`The ${this.name} is ${age}`)
}
 sayHi.call(person, 21) sayHi.bind(person, 21) Copy the code
  • A: undefined is 21 Lydia is 21
  • B: function function
  • C: Lydia is 21 Lydia is 21
  • D: Lydia is 21 function
The answer

Answer: D

Using both methods, we can pass objects that we want the this keyword to reference. However,.call is “executed immediately.”

.bind returns a “copy” of the function, but with a binding context! It is not executed immediately.


34. “What is the output of the following code?”

function sayHi() {
  return (() = > 0) ()}

typeof sayHi()
Copy the code
  • A: "object"
  • B: "number"
  • C: "function"
  • D: "undefined"
The answer

Answer: B

The sayHi method returns the return value of the execute now function (IIFE). This immediate function returns 0 and is of type number

Reference: There are only seven built-in types: NULL, undefined, Boolean, number, String, Object, and symbol. Function is not a type. A function is an object. Its type is Object.


35. “What is the output of the following code?”

0
new Number(0)
(' ')
(' ')
new Boolean(false)
undefined Copy the code
  • A: 0.' '.undefined
  • B: 0.new Number(0).' '.new Boolean(false).undefined
  • C: 0.' '.new Boolean(false).undefined
  • D: All of them are falsy
The answer

Answer: A,

There are only 6 falsy values:

  • undefined
  • null
  • NaN
  • 0
  • ' ' (empty string)
  • false

Function constructors, such as New Number and New Boolean, are truthy.


36. “What is the output of the following code?”

console.log(typeof typeof 1)
Copy the code
  • A: "number"
  • B: "string"
  • C: "object"
  • D: "undefined"
The answer

Answer: B

Typeof 1 returns “number”. Typeof “number” Returns “string”.


37. “What is the output of the following code?”

const numbers = [1.2.3]
numbers[10] = 11
console.log(numbers)
Copy the code
  • A: [1, 2, 3, 7 x null, 11]
  • B: [1, 2, 3, 11]
  • C: [1, 2, 3, 7 x empty, 11]
  • D: SyntaxError
The answer

Answer: C

JavaScript creates something called “Empty slots” when you set values for an array that exceed the length of the array. Their values are actually undefined. Here’s what you’ll see:

[1, 2, 3, 7 x empty, 11]

This depends on your running environment (each browser, and node environment, may be different).


38. “What is the output of the following code?”

((a)= > {
  let x, y
  try {
    throw new Error(a)  } catch (x) {
 (x = 1), (y = 2)  console.log(x)  }  console.log(x)  console.log(y) }) ()Copy the code
  • A: 1 undefined 2
  • B: undefined undefined undefined
  • C: 1 1 2
  • D: 1 undefined undefined
The answer

Answer: A,

The catch block receives the parameter X. When we pass parameters, this is different from the variable x we defined earlier. This x is in the catch block-level scope.

We then assign a value of 1 to the variable in the block-level scope and also set the value of the variable y. Now, we print the variable x in the block-level scope with a value of 1.

The value of x outside the catch block is undefined and y is 2. When we execute console.log(x) outside of the catch block, undefined is returned and y returns 2.


39. “What is the output of the following code?”

  • A: Basic types and objects
  • B: Functions and objects
  • C: Only objects
  • D: Numbers and objects
The answer

Answer: A,

JavaScript only has basic types and objects.

Basic types include Boolean, NULL, undefined, bigint, number, string, and symbol.


40. “What is the output of the following code?”

[[0.1], [2.3]].reduce(
  (acc, cur) = > {
    return acc.concat(cur)
  },
  [1.2]
) Copy the code
  • A: [0, 1, 2, 3, 1, 2]
  • B: [6, 1, 2]
  • C: [1, 2, 0, 1, 2, 3]
  • D: [1, 2, 6]
The answer

Answer: C

[1, 2] is the initial value. The initial value will be the value of the first parameter acc when first called. On the first execution, acc has the value [1, 2] and cur has the value [0, 1]. Combine them and the result is [1, 2, 0, 1]. On the second execution, acc has the value [1, 2, 0, 1] and cur has the value [2, 3]. Combine them and the end result is [1, 2, 0, 1, 2, 3]


The last portal

The last portal

โค๏ธ thank you

If you find this article helpful:

  1. Click “like” to support it, so that more people can see this content.

  2. Share your thoughts with me in the comments section, and record your thought process in the comments section.

  3. If you feel good, you can also check out the previous article:

    [[full of sincerity ๐Ÿ‘]Chrome DevTools debugging tips, efficiency โžก๏ธ๐Ÿš€๐Ÿš€ โžก๏ธ

    [Dry goods ๐Ÿ‘] From detailed manipulation of JS arrays to an analysis of array.js in V8

    [1.2w word ๐Ÿ‘] To his girlfriend’s secret – how the browser works (on)

    [1.1W word] To his girlfriend’s secret – how the browser works (rendering process) chapter

    [suggestion ๐Ÿ‘] Again 100 JS output questions sour cool continue (total 1.8W words + consolidate JS foundation)

    The 9 basic operations of the “Algorithms and Data Structures” list

This article is formatted using MDNICE