microvium icon indicating copy to clipboard operation
microvium copied to clipboard

var variable undefined

Open boogie opened this issue 1 year ago • 3 comments

It seems there is an issue related to var declarations:

function main() {
    var x = 'before';
    console.log(x);
    var x = 'after';
}
main();

The expected behaviour is printing "before", while it prints "undefined".

This code work as expected:

function main() {
    x = 'before';
    console.log(x);
    var x = 'after';
}
main();

boogie avatar Apr 13 '23 12:04 boogie

My guess is that it's hoisted both var declarations separately rather than merging them.

What are you trying to achieve with this code?

coder-mike avatar Apr 13 '23 22:04 coder-mike

I'm trying to avoid var, it is just that I've spotted a difference between official JavaScript and Microvium.

boogie avatar Apr 14 '23 00:04 boogie

Ok, thanks for pointing it out. I've added it to the to-do list.

coder-mike avatar Apr 16 '23 21:04 coder-mike