1

I was asked what happens when we assign a value to a variable?

Meaning, let's say we have a variable data and we assign some value to it,

var data = 5

What actually happens in the background? What is the initial value of data and what happens, how value is assigned?

I tried to google articles for it, but I couldn't find anything related to working, may be I was not using correct keywords. If anyone have any article that I could follow or provide me some explanation that would be really great.

3
  • How in-depth is the explanation supposed to be? I'd normally just say that data is created first as undefined and then assigned a 5. But are you looking for how that code gets interpreted at an even lower level by V8 specifically? Commented May 28, 2021 at 5:12
  • 3
    Is this about hoisting? var data gets defined at the top of the scope and the value gets assigned when this particular line runs? Commented May 28, 2021 at 5:12
  • @adiga Yes, thank you. This is exactly what I wanted. Actually, I didn't exactly understand at that time what was actually asked to me but looking at article on hoisting gave me clarity. Commented May 28, 2021 at 5:17

2 Answers 2

4

Because you are using var, the variable named data is first created by the interpreter at the start of the containing function block and its initial value at that point is undefined. This concept of moving the declaration to the start of the function block is called variable hoisting. This data variable will be a piece of memory internal to the interpreter. It will probably be allocated within a stack frame object.

A reference on hoisting:

Hoisting on MDN

Basics of hoisting in Javascript

Note also that functions declared like this:

function x() {
    // code here
}

Are also hoisted so two functions in the same scope can call each other without worrying about declaration order.

When the interpreter gets to the point of executing the code where the declaration/assignment line is located, it will assign the value of 4 to the variable.

So, if your code was like this:

function myFunction() {
    var sum = 3 * 4;
    console.log(sum * 3);
    var data = 5;
    console.log(sum * data);
}

myFunction();

Then, what the interpreter actually does internally is this:

function myFunction() {
    var sum = undefined, data = undefined;
    sum = 3 * 4;
    console.log(sum * 3);
    data = 5;
    console.log(sum * data);
}

myFunction();

In fact, you can even write this (somewhat ugly) code:

function myFunction() {
    var sum = 3 * 4;
    data = 6;               // this will not be an error
    console.log(data);
    console.log(sum * 3);
    var data = 5;           // the declaration of data is hoisted
    console.log(sum * data);
}

myFunction();

because, this is how the interpreter handles that:

function myFunction() {
    var sum = undefined, data = undefined;
    sum = 3 * 4;
    data = 6;               // this will not be an error
    console.log(data);
    console.log(sum * 3);
    data = 5;
    console.log(sum * data);
}

myFunction();

Note that let and const are block-scoped, not function-scoped and, if you try to reference them before their declaration, it is an error whereas var lets you do that. var is somewhat legacy and there is rarely (if ever) a reason to use var any more. It is safer to code with let and const instead. If you want function scope, you can still put your let or const at the top level of your function. But, you should generally declare your variables within the block that they are used/needed (restrict the scope to only what is needed).

Sign up to request clarification or add additional context in comments.

Comments

0

The cpu will try to find a location in memory to hold the value of 5 and store the value in that address and store the address in the variable declared by var.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.