This is the 9th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

The purpose of this series of interviews is to learn the basics so that you can easily answer questions in various forms. This article covers some of the most commonly asked questions in JavaScript.

If you feel helpful, please click 👍 to encourage you

  • JavaScriptWhat kind of language is it?
  • JavaScriptHow many data types are there?
  • What are value types and reference types? What are they?
  • What are imaginary and true values? What are they?
  • &&||What can an operator do?

What kind of language is JavaScript?

  • A static language is one that needs to confirm its variable type before use.
  • Languages that need to check data types during runtime are called dynamic languages;
  • Languages that support implicit type conversions are called weakly typed, and vice versa.
  • JavaScriptBelong toDynamic languages.Weakly typed language.

How many data types does JavaScript have?

Eight kinds, respectively:

  • Number
  • String
  • Boolean
  • undefined
  • null
  • Object
  • Bigint
  • Symbol

The types we often use, such as array and function, belong to the complex data type Object.

What are value types and reference types?

Storage location

Variables of value type are stored in stack memory. If you declare a variable of value type in a function, it is automatically destroyed when the function is finished executing.

The name of a variable of a reference type is stored in stack memory, but the value of the variable is stored in heap memory. A variable of a reference type is not destroyed automatically. When no reference variable refers to it, the garbage collection mechanism of the system collects it.

Assignment way

Direct assignments to variables of value type are deep assignments. Changing the value of B does not affect A.

function foo() {
    let a = 1
    let b = a
    a = 2
    console.log(a); / / = > 2
    console.log(b); / / = > 1
  }
foo()
Copy the code

A direct assignment to a variable of a reference type actually passes a reference, but only a shallow assignment. Changing the value affects all variables that reference that address.

function foo() {
  let c = {
    name: 'warlber'
  }
  let d = c
  d.name = A Warbler's Tail
  console.log(c.name); //=> a bird
  console.log(d.name); //=> a bird
}
foo()
Copy the code

Add properties and methods

A value type cannot add properties and methods.

function foo() {
  let a = 1
  a.name = 'warbler'
  console.log(a.name); //=> undefined
}
foo()
Copy the code

Reference types can add properties and methods.

function foo() {
  let a = {}
  a.name = A Warbler's Tail
  console.log(a.name); //=> a bird
}
foo()
Copy the code

What are the value types and reference types?

The basic value types are: undefined, Boolean, Number, String, Symbol.

The reference type can be Object.

Special reference type: null, pointer to an empty address.

The special reference type: function is not used to store data, so there is no such thing as copying functions.

What are imaginary and true values? What are they?

Simply put, an imaginary value is a value that becomes false when converted to a Boolean value, and a value that becomes true.

How do I check if a value is virtual? Use Boolean functions or!! Operator.

Virtual value (Falsy)

  • The value is a string of 0 length
  • 0
  • false
  • undefined
  • null
  • NaN

The true value (Truthy)

  • An empty array
  • An empty object
  • other

&& and | | operators to do?

Also known as logic and, it uses short circuits to prevent unnecessary work.

  • Finds the first virtual-valued expression in its operands and returns it, or returns the last truth-valued expression if no virtual-valued expression is found.
  • The conditions for all operands aretrueWhen, the result istrue;
  • If I have a thetafalseAnd the result is zerofalse;
  • When the first condition is zerofalseWhen, the following conditions are no longer judged.
console.log(false && 1 && []); // false
console.log("" && true && 5); / / 5
Copy the code

useifstatements

if(value){
 console.log(value)
}
Copy the code

use&&The operator

value && console.log(value)
Copy the code

| | also called or logic or, this is also used by the short circuit to prevent unnecessary work.

  • Finds the first truth expression in its operands and returns it;
  • As long as there is one conditiontrue, the result is zerotrue;
  • When both conditions are zerofalseWhen, the result isfalse;
  • When a condition is zerotrueWhen, the following conditions are no longer judged;
  • In support ofES6It is used to initialize default parameter values in the function before default function parameters.
console.log(null || 1 || undefined); / / 1
function logName(name) {
  var n = name || "Mark";
  console.log(n);
}
logName(); // "Mark"
logName('hzw'); // "hzw"
Copy the code

Logical and takes precedence over logical or

Such as the console. The log (3 | | 2 & 5 | | 0), will be 2 & 5 has a value of 5, then 3 | | 5-3, then 3 | | 0-3, so the final result is 3.

Expression a && expression b

Evaluates the result of expression A (which can also be a function), if True, executes expression B (or function) and returns the result of b; If False, return the result of a.

Expression a | b | expression

Evaluates the result of an operation on expression A (which can also be a function) or, if Fasle, executes expression B (or function) and returns the result of B; If True, return the result of a.