Enhancing one's comprehension of precompiling JavaScript

var foo=1;
function bar(){
  foo=10;
  return;
  function foo(){}
}
bar();
alert(foo);

As I delve deeper into the inner workings of JavaScript running on the machine, I stumbled upon this code snippet. Despite my best efforts, I am perplexed as to why the final alert displays 1 instead of 10. If anyone could shed some light on how the JavaScript virtual machine processes these lines of code, I would greatly appreciate it. Thank you!

Answer №1

The reason for this behavior is function declaration hoisting:

var x = 1;
function y(){
  function x(){} // The engine moves this function declaration up
  x = 10; 
  return;
}
y();
alert(x); // Since the local variable x was reassigned, not the global one, it remains 1

Answer №2

I am puzzled as to why the final alert shows a value of 1 instead of 10.

This discrepancy occurs due to the foo variable within the function bar:

foo = 10;

...being associated with the function declaration later in that same function:

function foo(){}

...rather than the foo variable outside of that function. Specifically, this is referencing:

var foo=1;
function bar(){
  foo=10;           
  return;
  function foo(){}    
}
bar();
alert(foo);

The reason for this behavior can be attributed to two factors:

  1. Function declarations are immediately processed upon entering the containing scope (in this case, the call to bar), before any step-by-step execution of code within the function. This phenomenon, sometimes referred to as "hoisting," ensures that all declarations are handled beforehand regardless of their placement in the code.

  2. Function declarations effectively create variables with identical names as the functions themselves. Therefore, the foo referenced in the function declaration acts as a variable with that name - thereby allowing assignment of new values to these "variable-like things."

When the provided code runs, here's the sequence of actions performed by the JavaScript engine:

  1. Creates a variable named foo with an initial value of undefined.

  2. Defines the bar function, registering it as an in-scope symbol within the current context and associating it with the function definition.

  3. Begins executing the code step-by-step within this scope.

  4. Sets the value of foo to 1.

  5. Invokes the bar function.

  6. Establishes the foo function relevant to the bar call, creating it as an in-scope symbol during the function invocation.

  7. Initiates the step-by-step processing within this nested scope.

  8. Assigns the value 10 to the local foo, which previously referenced the function itself.

  9. Exits from the function.

  10. Executes the alert function using the foo within this specific scope, still holding the original value of 1.

Further insights into this intricate process can be found in Section 10.4.3 of the ECMAScript specification along with its related sections.


* "variable-like thing": In JavaScript, every execution context (comprising the global context and all contexts resulting from function calls) maintains a binding object to manage various identifiers and their corresponding values within that specific context. The binding object incorporates properties for each variable, function declaration, and other essential elements like the arguments pseudo-array and the function name referring back to itself. Consequently, changing the value of foo within bar overrides the reference to the foo function declared inside bar rather than modifying the outer-scoped variable. Essentially, foo behaves akin to a local variable within bar due to the function declaration.

Answer №3

This topic relates to a programming concept known as hoisting. When you define a function using function foo, it is basically just another way of writing var foo = function ... So, within the bar function, the name foo actually refers to a local variable that shadows the outer foo variable. Initially, this foo is a function, but it later gets redefined as the number 10.

With hoisting, the name foo is "reserved" and scoped during the parsing phase before the code is executed. In essence, it operates as follows:

function bar(){
  var foo = function () {};
  foo = 10;
  return;
}

Therefore, it doesn't overwrite the outer variable at all.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Resizing svg to accommodate a circle shape

As I work on my vue.js app that involves a plethora of diverse icons, I made the decision to create a small icons builder in node.js. The purpose is to standardize their usage and also "crop" each SVG so it fits perfectly within its parent container by uti ...

Generate arrays with custom names by parsing JSON data

Currently, I am retrieving data from a MySQL database and converting it to JSON before passing it to a JavaScript class for chart display purposes. The challenge lies in creating arrays required by the chart from the JSON object without manually creating a ...

Is your $http request causing an XML parsing issue?

I'm attempting to utilize the $HTTP method from angularJS to access my restful web service. On entering my web service URL in the browser, I receive a result of APPLICATION/JSON type. {"id":20418,"content":"Hello, World!"} The URL for my web servic ...

What is the best way to send the selected option from a dropdown to a button click function within the controller?

I need to pass the selected value from a user's country selection dropdown to the ng-click="saveChanges()" function on the submit button. Is there a correct method for achieving this? I want to be able to access the user's selection from the dro ...

"Despite the null date in Node.js, the validation for expiration dates less than Date.now() is still being enforced

I am currently working on implementing validation for evaluating the finish status. However, my validation is encountering a problem with the "null" value of expiresAt. It should indicate that the evaluation has been successfully completed. The issue lie ...

Looking to fill every space? Try using React with MUI Grid!

I am currently using Material UI Grid to create an image grid, and I am facing challenges in eliminating the gaps between certain grid items. Despite attempting to modify the alignitems and justify values of the container, I have not been successful in r ...

Is there a way to allow only the block code to shift while keeping the other span tags stationary?

Is it possible to change the text without affecting other span tags in the code? I want to make sure only this specific text is updated. How can I achieve that? var para_values = [ { content: "BRAND " }, { content: "MISSION" } ]; functi ...

Modify the information and return it to me

I am attempting to modify and return the content inside a custom directive (I have found some resources on SO but they only cover replacement). Here is an example: HTML <glossary categoryID="199">Hello and welcome to my site</glossary> JS . ...

Creating a reusable anonymous self-invoking function

Here is a function that I am working with: (function(e, t) { var n = function() { //code, code, code }; //code, code, code e.fn.unslider = function(t) { //code, code, code }; })(jQuery, false) To execute this function, I have impleme ...

What is the best way to incorporate CSS into JavaScript code?

Here is the code I am working with: <script type = "text/javascript"> function pic1() { document.getElementById("img").src = "../images/images/1.png"; } function pic2() { document.getEl ...

Getting Rid of Angular Material Suggestions: A Step-by-Step Guide

<md-autocomplete ng-model="ctrl.searchText" md-selected-item="ctrl.selectedItem" md-selected-item-change="ctrl.selectedItemChange(item)" md-search-text="ctrl.searchText" md-search-text-change="ctrl.searchTextChange(ctrl.searchText)" ...

Discover the method to calculate the combined file size of multiple uploads with jquery

One of the challenges I'm facing is ensuring that users can only upload multiple files if the total size does not exceed 3GB. Is there a way I can enforce this limit? Below is the current code snippet I am using: var fileCount = 0; var showFileCo ...

Encasing newly inserted child components within my designated slot

If I have a component with a single slot and instead of simply rendering all its children, I want to wrap each element individually, the following approach can be taken: Vue.component('MyElement', { render: function(createElement){ for (l ...

Validating a string using regular expressions in JavaScript

Input needed: A string that specifies the range of ZIP codes the user intends to use. Examples: If the user wants to use all zip codes from 1000 to 1200: They should enter 1000:1200 If they want to use only ZIP codes 1000 and 1200: They should enter ...

Issues with Javascript functionality on aspdotnetstorefront site

Lately, I've been facing some challenges with my Javascript and jQuery codes when trying to implement them in the storefront. Despite attempting different methods to create a slider, none seem to work within the store environment - even though they fu ...

Incorporating graphics into a React component

Currently exploring React JS and looking to dive into the practical side of things. Following a documentation tutorial that constructs a basic comment system. I've replicated the component structure outlined in the tutorial: PostBox PostList Pos ...

Using VueJs's createElement() to dynamically insert HTML content

I am currently working on a component that converts all icons to SVG format. By the end of my code, I have included the following: return createElement('i', '<SVG>CODE</SVG>' ) In the spot where the SPA ...

Having trouble making the child process die in NodeJS fork

I'm facing a roadblock here, perhaps it's just a minor issue that I can't seem to solve because of my lack of experience with NodeJS. Currently, I am developing a Bluetooth device that will be controlled by a master application. For prototy ...

A comprehensive guide on personalizing Bootstrap 4 tooltips to suit your specific needs

I would like to customize the tooltip in Bootstrap 4 based on the screenshot provided below: https://i.stack.imgur.com/wg4Wu.jpg <!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap Example</title> <meta chars ...

What is the best way to update and add data with just one click using jQuery?

Trying to dynamically add text to textboxes by clicking an "add" button. The code allows for adding/removing textboxes that save values to and delete from a database. Upon page load, a function called showitem creates textboxes dynamically and populates th ...