-1

I'm new to coding and I started with javascript. It's been a smooth journey for me but I have a question, why does I see people call function before define it first?

Example:

doSomething();

function doSomething(){
    console.log("Hello World");
}

Not like this?

function doSomething(){
    console.log("Hello World");
}

doSomething();

Thank you.

  • Is this really a why question? Or are you pointing out that using something before it's declare isn't normally how things are done in programmer? – Rocky Sims Sep 24 '22 at 06:39
  • 1
    This is a good beginner question: not all languages support what JavaScript does here. The term is [hoisting](https://developer.mozilla.org/en-US/docs/Glossary/Hoisting), and though the question I suggested as duplicate is a little more specific than yours, I hope it points you in the right direction. – Jeff Bowman Sep 24 '22 at 06:40
  • Thank you for your answers. I'm actually new to this and I'm not trying to point out anything, I'm simply just curious about it and now I know what about it. Thanks – New Developer Sep 24 '22 at 11:23

1 Answers1

-2
doSomething();

function doSomething(){
    console.log("Hello World");
}
function doSomething(){
    console.log("Hello World");
}

doSomething();

both are same . when the javascript debugger comes to this line doSomething() then he will go to find that function and if he will not able to find that then it will show an error .

another concept is

    function doSomething(){
        console.log("Hello World");
}

this total function will not compile until the compiler/javascript debugger find the decalared function that is doSomething() .

Rahul Mohanty
  • 342
  • 3
  • 9