Skip to content
Search

Hoisting

lastmod 2024-11-06created 2022-12-084 edits

In a word, pulling up.

Most notably, it refers to the interpreter in JavaScript allocating memory space for variables and functions in advance, before their declarations.

About

hoisting (noun): pulling up, lifting and carrying.

Put simply, as the word suggests, it is the phenomenon where variable and function declarations in a script seem to be 'pulled up~~' regardless of their declaration order. The point is that they are not physically 'pulled up'; this happens because the JavaScript engine first scans the entire code once and records them in the execution context in advance.

An example of hoisting a function makes the concept of hoisting easy to understand.

test(); // 이 코드가 과연 실행될까..?

function test() {
  document.writeln("Hoisting");
}

test();

When the function test(){...} code that assigns the function is written below the test() code that calls it, thinking about it in theory without the concept of hoisting, the test() on the very first line should not run.

However, because JS hoists functions (pulls them up~~!!), the test() code at the very top also works fine.

in Rust

const can be used regardless of declaration order.

See also

Favorite site

author Zer0 sitemap