- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
<html> <head> <title>JavaScript String match() Method</title> </head> <body> <script> // all the variables declared at top var str, re, found; str = "For more information, see Chapter 3.4.5.1"; re = /(chapter \d+(\.\d)*)/i; found = str.match( re ); document.write(found ); </script> </body> </html>
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.
Advertisements