I Hate Putting Semicolons, Don't You?
2 days ago
1 min read

I Hate Putting Semicolons, Don't You?

Hello Everyone, Welcome to my blog 🙏.

I hope you are all safe and that this blog finds you in good health ❤️.

I HATE KEEPING SEMICOLONS, DON'T YOU?

But, After looking at this, I realised Semicolons are really needed

const callMe = function (word) {
    console.log(word);
}
const myInfo = function() {
    return callMe.bind(this, "Abhishek");
}
(function() {
    console.log("Hello there")
})()

Can someone expect what will be the output for this?

HAHA!! I know I know this is quite simple.

The answer is "Hello there" right?

TBH, I thought the same but the result is something different we get the result as "Abhishek"

What?

we didn't call callMe function anywhere then how that is possible?

Let me explain this

you see there is IIFE at the end,

First, the function myInfo returning callMe is called with the second function as a parameter, and then the "Abhishek" gets printed Now.

How would that be related to SEMICOLON?

Just by putting a semicolon, we would separate each function expression which means whatever is our expected result i.e. "Hello There" gets printed.

const callMe = function (word) {
    console.log(word);
};
const myInfo = function() {
    return callMe.bind(this, "Abhishek");
};
(function() {
    console.log("Hello there")
})();

Note: there is no need of putting semicolons after the normal functions but, for functional expressions it is better to have to avoid unexpected results.

For Example:

function callMe(word) {
    console.log(word);
}
function myInfo() {
    return callMe.bind(this, "Abhishek");
}
(function() {
    console.log("Hello there")
})();

This works as expected, we would get "Hello there" as a result.

USES OF SEMICOLON

  • Easy to debug the code.

  • Leverage readability in the code.

----------------------------------------------------------------------------------------------------------

Here we go, That’s it folks for this blog.

Hope everyone liked it.

Give it a like ❤️, and share it with your friend.

For more exciting content on Frontend,

Please do follow me 🎯.

Thanks a lot, Everyone 🙏. Until Next time, Happy Learning ✍️

Abhishek Kovuri, UI developer

Appreciate the creator