loading...

What would this JS code print? 🤔

haseebelaahi profile image Haseeb ・1 min read

What do you think this piece of code will print? Most of you might know the answer but the behavior is really interesting to me and got me thinking and learning about a very important concept in JS.

function saySomething() {
    var greeting = "Hello";
    {
        greeting = "Howdy";
        let greeting = "Hi";
        console.log(greeting);
    }
}
saySomething(); //what would this print?

Discussion

pic
Editor guide
Collapse
invot profile image
invot

error. It returns an error.
developer.mozilla.org/en-US/docs/W...

Collapse
swssr profile image
Simo

Console error I think invalid token.

Collapse
techie448 profile image
techie448

Interesting, could you help understand why it is throwing an error?