This is the 14th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge

When sorting out the interview questions today, I think I will do the interview questions at the beginning of the interview. Most of the questions are actual combat, not simple back definition. Today I will look at some questions to review the relevant knowledge points.

Variable lifting problem

function info() {
   console.log(name);
   console.log(age);
   var name = 'luxuriant';
   let age = 24;
}
Copy the code
  • A: luxuriant undefined
  • B: Luxuriant ReferenceError
  • C: ReferenceError 24
  • D: undefined ReferenceError

Answer: D

Do you remember the definition of familiar, encountered code, suddenly will not analyze the code, let’s analyze this code:

In the info function, we declare the name variable using var. JavaScript allocates memory for name during variable creation. The default value is undefined, so when we print the name, the value is undefined.

This is called a “temporary dead zone”. In this case, JavaScript will throw a ReferenceError.

Summary — Variable assignment is divided into three stages:

  1. Create variables, open up space in memory;
  2. Initialize the variable. Initialize the variable toundefined;
  3. The assignment

Let, var, function:

  1. The “create” process for a let is promoted, but initialization is not.

  2. Var “create” and “initialize” have been improved.

  3. Function ‘create’, ‘initialize’, and ‘assign’ have all been upgraded.

Arrow functions and ordinary functions

const numValue = {
  num: 10;
  numTwo() {
    return this.num * 2
  },
  numThree:() = > this.num * 3
};

numValue.numTwo();
numValue.numThree();
Copy the code
  • A: 20 30
  • B: 20 NaN
  • C: NaN 30

Answer: B

In this case, the “this” in the arrow function refers to the context, so when you call numThree, you’re not referring to the numValue object, you’re referring to the environment (window) that it defines. In this case, num is undefined. Normal functions still follow who calls whom.

The operator

+true;
!"WEIRUI";
Copy the code
  • A: 1 false
  • B: false NaN
  • C: false false

Answer: A,

The unary plus operator converts Boolean to numeric, true to 1, false to 0; The string “WEIRUI” is a truth value. The code is asking if the truth value is false, so it returns false.

Operator 2

let num = 0;
console.log(num++);
console.log(++num);
console.log(num);
Copy the code
  • A: 1 ` ` 1 ` ` 2
  • B: 1 ` ` 2 ` ` 2
  • C: 0 ` ` 2 ` ` 2
  • D: 1 0 ` ` ` ` 2

Answer: C

Suffix unary operator++:

  1. Return value (return0)
  2. Increment value (num is now1)

Prefix unary operator++:

  1. Increment value (num is now2)
  2. Return value (return2)

So return 0, 2, 2.

Type conversion

function num(a, b) {
  return a + b;
}

num(1."2");
Copy the code
  • A: NaN
  • B: TypeError
  • C: "12"
  • D: 3

Answer: C

As we all know, JavaScript is a dynamically typed language. When we do not specify the type of a variable, the value is automatically converted to another type. This is called implicit typing.

In this example, JavaScript converts the number 1 to a string, making the num function meaningful and returning the value. When the number and string types are added, the number is implicitly converted to a string, returning “12”;