Two methods of function definition

Why is 1 printed here instead of 2 at the back

Jul.12,2022

this involves the declaration promotion mechanism in js. You define the function demo in two ways, the first is the function expression (lines 6-8), and the second is the function declaration (lines 9-11). The function created by the function declaration is explicitly 'advanced' to the top of the script, so for js, the processing order is as follows:

//  
function demo () {
  console.log(2)
}
var demo = function () {
  console.log(1)
}

demo()

the difference between function declaration and function expression in JS and the order of execution
are significantly different:
the first declaration method is var declaration (function expression). The function can only be called after the var statement declaration. The second declaration method is function declaration (function declaration). Functions can be called
before function declaration because of the first case. Function expressions are assigned to the variable demo only when the function is running (take your code as an example). In the second case, the function expression is assigned to the identifier demo

before the code runtime, that is, the code parsing phase

look at the following example:

  

declaration promotion: all declarations, including variables and functions, are processed first before any code is executed.
Note:
(1) the promotion of function declarations takes precedence over the promotion of variable declarations.
(2) duplicate var declarations will be ignored, but duplicate function declarations will overwrite the previous declaration.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1ec0464-2abf.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1ec0464-2abf.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?