Hoisting in JavaScript and Coding Convention .…#30daysofjavascript => Day 13
I will combine two different topics in today's episode because they are seemingly short.
Hoisting
By default, JavaScript moves declarations to the top of the scope. Would you be surprised to know that you can use variables before even declaring them? This is because JavaScript automatically moves this variable to the top This process is known as Hoisting.

What is Hoisting?
The term hoisting is a behaviour in JavaScript that moves a variable or variables to the top of the script. Interestingly, this only works with the var keyword. Let and Const keywords will give reference and syntax errors respectively.


What else do we know about hoisting? JavaScript only hoists declarations, not initializations. It will be impossible to access variables if they are initialized after the execution code. Look at the example below:

Coding Convention

Coding conventions are style guidelines for programming. The sore purpose is to improve readability and make code maintenance easier. The following are rules or guidelines to follow in JavaScript programming:
Variables
- Variable names should start with a letter.
- Use camelCase where you think there might be a need for spacing when naming variables or functions. e.g. firstName and lastName.
- Variable and function names are written as camelCase
- Global variables are written in UPPERCASE ( it’s quite common)
- Constants (like PI) written in UPPERCASE
Operators
- Always add spaces around operators (*, +, — , /) and after commas.
- Always use 2 spaces for indentation of code blocks.
Statements
- Always end a simple statement with a semicolon.
- Put the opening bracket at the end of the first line.
- Use one space before the opening bracket.
- Put the closing bracket on a new line, without leading spaces.
- Do not end a complex statement with a semicolon.
Object Rules
- Put the opening bracket on the same line as the object name.
- Use colon plus one space between each property and its value.
- Only use quotes around string values.
- Do not add a comma after the last property-value pair.
- Place the closing bracket on a new line, without leading spaces.
- Always end an object definition with a semicolon.

File Extensions
- HTML files should have a .html extension.
- CSS files should have a .css extension.
- JavaScript files should have a .js extension.
Use Lower Case File Names
- Most web servers (Apache, Unix) are case sensitive about file names: myphoto.jpg cannot be accessed as Myphoto.jpg.
- Other web servers (Microsoft, IIS) are not case sensitive: london.jpg can be accessed as London.jpg or london.jpg.
- If you use a mix of upper and lower case, you have to be extremely consistent.
- If you move from a case insensitive to a case sensitive server, even small errors can break your website. To avoid these problems, always use lower case file names (if possible).
Performance
- Coding conventions are not used by computers. Most rules have little impact on the execution of programs.
- Indentation and extra spaces are not significant in small scripts.
What is this about?
30daysofjavascript is a series of writing on how I learn to code in JavaScript. These episodes are as simplified as possible and for beginners like me, I hope you find JavaScript less confusing throughout this episode. Thank you as always and see you in the next episode. Check out every episode I have written here.