English

JavaScript Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables or classes to the top of their scope, prior to execution of the code.

Hoisting allows functions to be safely used in code before they are declared.

Variable and class declarations are also hoisted, so they too can be referenced before they are declared. Note that doing so can lead to unexpected errors, and is not generally recommended.

Function hoisting

One of the advantages of hoisting is that it lets you use a function before you declare it in your code.

Variable hoisting

Hoisting works with variables too, so you can use a variable in code before it is declared and/or initialized.

However JavaScript only hoists declarations, not initializations! This means that initialization doesn't happen until the associated line of code is executed, even if the variable was originally initialized then declared, or declared and initialized in the same line.

Until that point in the execution is reached the variable has its default initialization (undefined for a variable declared using var, otherwise uninitialized).

Readmore: https://developer.mozilla.org/en-US/docs/Glossary/Hoisting

 

Vietnamese

JavaScript Hoisting đề cập đến quá trình theo đó trình thông dịch dường như di chuyển phần khai báo của các hàm, biến hoặc lớp lên đầu phạm vi của chúng, trước khi thực thi mã.

Hoisting cho phép các hàm được sử dụng an toàn trong mã trước khi chúng được khai báo.

Các khai báo biến và lớp cũng được lưu trữ, vì vậy chúng cũng có thể được tham chiếu trước khi chúng được khai báo. Lưu ý rằng làm như vậy có thể dẫn đến các lỗi không mong muốn và thường không được khuyến khích.

Function hoisting

Một trong những lợi thế của việc nâng cấp là nó cho phép bạn sử dụng một hàm trước khi bạn khai báo nó trong mã của mình.

Variable hoisting

Hoisting cũng hoạt động với các biến, vì vậy bạn có thể sử dụng một biến trong mã trước khi nó được khai báo và / hoặc khởi tạo.

Tuy nhiên JavaScript chỉ lưu trữ các khai báo chứ không phải khởi tạo! Điều này có nghĩa là việc khởi tạo không xảy ra cho đến khi dòng mã liên quan được thực thi, ngay cả khi biến ban đầu được khởi tạo sau đó được khai báo hoặc được khai báo và khởi tạo trong cùng một dòng.

Cho đến khi đạt đến thời điểm thực thi, biến có khởi tạo mặc định của nó (không xác định cho một biến được khai báo bằng var, nếu không thì chưa được khởi tạo).

Readmore: https://developer.mozilla.org/en-US/docs/Glossary/Hoisting