This article summarizes JS in daily use and interview need to pay attention to the knowledge points.

Variable ascension

Look at the following code output what

    var myname = 'hello world'
    function showName() { 
        console.log('Function showName executed')}Copy the code

The answer:

The answer is somewhat different from the top-down execution order of JS in our impression. This is because JS omitted the variable declaration part for the convenience of developers to write code, and this part will be completed in the compilation stage, and developers are used to the “concise version”, and continue the “concise version” of the idea and the illusion. Now, we can restore the omitted declaration code, and we can see the clues:

First look at the “big picture” of variables:

var myname = 'hello world'

    var myname // Declaration section
    myname = 'hello world' // Assignment section

Copy the code

Now look at the “big picture” of the function:

// Bring the declaration forward
function showName() { 
   console.log('Function showName executed')}// Execute

Copy the code

Here, the above function showName is a complete function declaration with no assignment, whereas the following function declaration is declared separately from assignment:

var showName = function(){
    console.log("Function showName is executed.")}Copy the code

Equivalent to the following operations:

/ / declare
var showName = undefined

/ / assignment
showName = function(){
    console.log("Function showName is executed.")}Copy the code

The so-called variable promotion refers to the “behavior” in which the JavaScript engine promotes the declaration part of variables and functions to the beginning of the code during the execution of JavaScript code. When a variable is promoted, it is given a default value, which is known as undefined.

The script tag

Take a look at the following code. What will be printed?

<script type="text/javascript">
Copy the code

Answer: Program error

Content contained in the <script> tag will be executed sequentially by the browser until </script> is encountered; Note that the “</script>” string does not appear anywhere in the code. Once it appears, the browser interpreter will assume that the code terminator has arrived and will not parse any further content. Replace it with “<\/script>” so that the browser escapes the content and the program runs normally.


There are only six types of data in JS: undefined, Null, Boolean, Number, String, and Object.

Take a look at the following code and ask what is returned

var message

Copy the code

The answer is obvious: an error was reported when an alert(age) was issued; Let’s look at the following code and ask what output:

var message

alert(typeof message)
alert(typeof age)

Copy the code

The answer may be unexpected, it will pop undefined twice, and no error.

It may seem strange, but it’s true; The typeof operator returns undefined for an uninitialized variable, and undefined for an undefined variable. Perhaps the logic behind this is that variables are never actually executed, whether they are defined or not


Look at the following code output what?

console.log(NaN= = =NaN)
Copy the code

The correct answer is false

NaN means: “Not a Number”; Is a special value that represents the case of an operand that does not return a value instead of an error. For example, “10/0” is a direct error in other languages, but JS added NaN to prevent the program from crashing. 10/0 = NaN, NaN and any number operation is NaN, for example, NaN * 10 will return NaN. Some people say, well, what do I do to determine if a variable value is NaN? In fact, JS provides built-in isNaN()

Function parameters

Look at the following code output what?

function sum(num1, num2){
    return num1 + num2

let a = sum(1)
Copy the code

Will it report an error? The correct answer is: Output NaN

The parameters of JS function are different from those of most languages. There are no mandatory restrictions on the type and number of parameters of JS function. Within JS, the implementation of function parameters is represented by an array; Therefore, even if you define two parameters, only one parameter is passed, it does not affect the normal operation of JS. The arguments object can be used to access the ARGUMENTS array inside the js function body while writing code to get each argument passed to the function.

The sum function takes two arguments, but only one argument is passed. Arguments [1] = NaN (not a number)

The function name

Look at the following code. If there are two functions with the same name, what will be output after the code is run?

function addNum(num) {
     return num + 10

function addNum(num) {
    return num + 20

let num = addNum(1)
Copy the code

Correct answer: Output 21

In other languages, such as Java, the same function name can be defined as long as the function signature or the type and number of function parameters are different. This is traditional overloading, but there is no overloading in JS. If two or more identical function names are defined, JS will take the function defined last.


If you take a look at the code, what is the final output?

    var num1 = 5
    var num2 = num1
    num2 = num2 + 1

    var obj1 = new Object(a)var obj2 = obj1 = "ryugou"
Copy the code

Correct answer:

Copy the code

Js variable values can be divided into two types: basic type and reference type; The basic type values are simple data segments, including underpay, Null, Boolean, Number and String, and the rest are reference types. These two types of values also differ when assigning values between variables. For variables whose value is a primitive type, a new value is copied to the assigned variable during assignment. For example, var num2 = num1 In this example, copy the value of num1 and fill it in to num2. However, if it is a reference type variable, the variables will still refer to the original reference when assigned. For example, var obj2 = obj1 in the code, where obj2 and obj1 both refer to the same object.

For more exciting content, please pay attention to my public number