- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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.
- Related Articles
- Is it a good practice to end switch with defaults in JavaScript?
- Why crop rotation is considered a good agricultural practice?
- Why is it not good practice to use date values with two-digits years in MySQL?
- Is it a good practice to remove grass and small plants that are growing in an open, unused field?
- Place the image at the top inside a Bootstrap 4 card
- Is the weather of a particular place same at all the time?
- The temperature at the foot of a mountain is $+$ 8°C. It fell down by 10°C at the top of the mountain. The temperature recorded at the top is?
- Why is it considered a bad practice to omit curly braces in C/C++?
- MySQL query to place a specific record on the top
- Check If It Is a Good Array in C++
- How to implement backtracking for a climbing stairs practice in JavaScript?
- Is diary writing a good habit at night?
- What is Cryogenic sleep and can it be put to practice practically?
- Top Qualities of a Good Manager and a Leader
- Is it a good idea to add sugar in black grape juice?

Advertisements