Is it a good practice to place all declarations at the top in JavaScript?

Yes, it is a good practice to place all the JavaScript declarations at the top. Let’s see an example −

Example

   
      JavaScript String match() Method
   

   
      
   

The following are the valid reasons −

  • Gives a single place to check for all the variables.
  • Helps in avoiding global variables
  • Re-declarations are avoided.
  • The code is easy to read for others as well.
Updated on: 2020-05-20T09:07:11+05:30

156 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements