Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7845385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:12:22+00:00 2026-06-02T17:12:22+00:00

The Javascript tutorial I follow always defines before using, but my PHP book always

  • 0

The Javascript tutorial I follow always defines before using, but my PHP book always defines at the end, and in fact points out that this is considered good practice.

Is there a reason to do it one way or the other?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T17:12:23+00:00Added an answer on June 2, 2026 at 5:12 pm

    If you have inline code executing (e.g. executing as it is loaded), then global variables must be defined before the code that uses them.

    Functions can be defined in whatever order you think makes the code the most tidy and easiest to read.

    For example, in this code:

    foo();
    
    function foo() {
        alert(x);
    }
    
    var x = 4;
    ​
    

    The call to foo() will alert undefined because x does not yet have a value when foo() is called, but you will notice that foo can be called in code that appears before the function definition because all functions are loaded before any code is actually executed.

    As for a best practice, I think it makes sense to organize your code in the best way you can find that puts modules of related functionality together, but the order is not generally material. I trust you realize that javascript doesn’t have anything that is actually a class. It can use function objects and prototypes to simulate some class-like behavior that other languages have, but it doesn’t really have classes as it’s objects are based on prototypes, not classes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I remember reading in a JavaScript tutorial that it's generally best to avoid using
So I'm no JavaScript genius, but I can follow a tutorial just fine. I've
I'm using the concepts taught in this tutorial to communicate with Javascript to and
I'm trying to follow the custom module tutorial at http://dojotoolkit.org/book/dojo-book-0-9/part-3-programmatic-dijit-and-dojo/modules-and-namespaces/creating-your-own-modul I've got a local
I have some JavaScript, which is generated by PHP code, using a template. The
I'm using Ruby on Rails 3.0.8. I'm trying to follow the tutorial by Ryan
In every JavaScript tutorial that I have looked there is something mentioned about a
I have a JavaScript slideshow that pre-loads images out of a MySQL database and
I'm just starting out using module patterns (thanks to Christian Heilmann's original tutorial) to
I followed the HTML5 Websocket tutorial of the website below: http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/ It worked well,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.