DEV Community

Haseeb
Haseeb

Posted on

What would this JS code print? 🤔

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?

Top comments (3)

Collapse
 
invot profile image
invot • Edited

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?