Urgent.News

What's breaking now, across thousands of outlets.

Tech

Hoisting in javascript

Hoisting is the default behaviour of javascript where all the variable and function declarations are moved on top. 1. Function Hoisting greet (); function greet () { console . log ( " Hello Sajin " ); } Output: Hello Sajin This works because function declarations are hoisted. 2. var Hoisting console . log ( name ); var name = " Sajin " ; Output: undefined JavaScript behaves roughly like: var name…

JavaScript's hoisting is the automatic behavior where variable and function declarations are moved to the top of their scope during the creation phase, before code execution. This means that all variable and function declarations are hoisted, regardless of where they are defined in the code.

Function hoisting occurs when a function is declared. For instance, when a function named greet() is declared, JavaScript automatically moves its declaration to the top of its scope. This allows the function to be called before it is defined in the code. An example of this is shown in the first point of the source material, where the function greet() is called and produces the output "Hello Sajin".

Variable hoisting, on the other hand, is a bit different. When a variable is declared with 'var', JavaScript moves the declaration to the top of its scope, but the assignment is not moved. This means that the variable can be accessed even before it is declared, but its value will be 'undefined' until it is assigned. This behavior is illustrated in the second point of the source material, where the variable 'name' is logged before it is assigned the value "Sajin".

Variables declared with 'let' and 'const' also undergo hoisting, but they cannot be accessed before their declaration. Similar to the 'var' keyword, their declaration is moved to the top of their scope, but their value assignment is not. This is demonstrated in the third point of the source material, where an attempt to log the value of 'name' before it is assigned results in a ReferenceError. The same principle applies to variables declared with 'const'.

The usefulness of hoisting is most evident in the case of function declarations. For example, the code can be structured so that the main logic of a function appears before its detailed implementation. The fourth point of the source material explains this with the example of a function named 'calculate', which is called before its definition.

The output of 30 is produced as a result of the addition of 10 and 20 within the function. This allows developers to organize their code in a way that makes it easier to understand and maintain.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

When every exception becomes a new role, you need attributes

If your permission matrix keeps growing a new role for every customer exception, that is usually a modeling problem, not a staffing problem.

  • Role-based access control breaks down with conditional rules.
  • User, resource, and environment attributes evaluate access.
  • Roles reserved for broad base, attributes for changing facts.

More from Tuesday 29 September →