T
T
TrainHard2015-11-07 11:35:41
JavaScript
TrainHard, 2015-11-07 11:35:41

Why is a number displayed and not undefined?

Hello!
Explain plz.

function num(i) {
  
  var i = i;
  console.log(i);

}
num(3);

Why does this example output 3 and not undefined?
I can not understand how the interpreter parses such code - in what sequence?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Zuev, 2015-11-07
@TrainHard

Maybe this post will shed some light on the situation.

for each variable (var)
- a VO property is created with the name of the variable, and the value is undefined; if a property with the same name already existed in the VO, it remains untouched.

A
Alexey Ukolov, 2015-11-07
@alexey-m-ukolov

So where does undefined come from?
After all, your code is fundamentally no different from this:

var foo = i;
console.log(foo);

Code in most programming languages ​​is parsed from right to left. A local variable and an argument are two different areas of memory, so one does not overwrite the other.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question