You should know this fact of JavaScript to write before code

This writing is about some facts about JavaScript that you should know before written code. So, I assume you have some basic knowledge about programming language.

Let's start…

We all know about data types, aren’t we? Basically, in JavaScript, there are two types of data types. Those are primitive and non-primitive.

Primitive Type

Primitive types are nothing just numbers, strings, and other things. All primitive types have common things that do not affect code. You might be want to know how many types primitive value is hidden in the JavaScript. After almost 25 years of research, the scientist discovers such types of primitive values. those are —

1. Undefined: It used for unintentionally missing values. Undefined is called lonely value type.

2. Null: It is used for intentionally missing values. Null is also called lonely value type.

3. Booleans: Its value is true or false. Booleans are used for logical operations.

4. Number: It's any kind of real number like 2, 3.5, -3.555222, 100 etcetera. It is used for mathematical calculations.

5. Strings: It’s nothing just a sequence of characters or a series of characters.

6. Symbols: It is used to hide implementation details.

7. BigInts: It’s nothing just Numbers. It is used for mathematical operations on big numbers.

Non Primitive Types

Non Primitive types are the rest of the others. In JavaScript, non-primitive types are Objects and Functions. You are surprised, aren’t you? Yes, there are no other fundamental value types in JavaScript. The rest are all is objects. For example Arrays, Date, Regular Expressions etcetera are fundamentally Object in JavaScript. You can check this to write this snipped code given below.

console.log(typeof([])) //object
console.log(typwof(new Date()) //object

As a programmer, you can write codes. But if your code is messy and not easily readable, it was not a good thing. Your code should neat and clean and easy to read as possible. It is actually an art of programming languages — how to take a complex task and convert it into code and human-readable. A good coding style will assist you. It will make your code look attractive and easily readable. I wrote some coding style for you.

Curly Braces

We all know about curly braces and used them frequently in our code. Sometimes we used it like this —

if(condition){
//your code here
}

or this —

if(codition)
{
//your code
}

But in JavaScript curly braces are written in “Egyptian” style. That means the opening curly brace on the same line, not a new line and there should use a white space before using an opening curly brace. like this —

if(condition) {
//your code
}
for(let i = 0; i < 5; i++) {
//your code here
}

Line Length

Usually, no one wants to read long horizontal code. Would you like to? I don’t think so. For best practice, split them into small lines. The maximum length of a line should be 80 to120 characters.

// back tick (`) helps string to slit multiple lines'
let string1 = `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.`

Indents

There are two types of indentation. Horizontal indent and Vertical indent.

1. Horizontal indent: The horizontal indent is made with 2 or 4 spaces or the key Tab. By pressing the tab rather than spaces allows a more flexible configuration of indents. For example, we can align the parameters like this —

function task(parameters,      
aligned,
one,
after,
another) {
// ...
}

2. Vertical indent: The vertical indent is an empty line for splitting into logical blocks. Vertical indent is very useful for more readability and makes it more clear to read codes. For example, we can divide a function into logical blocks by vertical indentation like this —

function task(n) {
let result = 1;
for (let i = 1; i <= n; i++) {
result *= i;
}
return result;
}

Add an extra new line this function split into logical blocks. It looks clean, isn’t it? There should not be more than nine lines of code without a vertical indentation.

Semicolones

A semicolon is used basically at the end of a statement. In JavaScript, putting a semicolon after a statement is optional but it should be used as a good practice.

Function Placement

We all declare functions in code to reduce redundancy. You can declare functions in three ways.

1. Declare all functions first then wrote the rest of the codes.

2. First wrote codes then declare all functions.

3. Or you can be mixed the mentioned two ways.

But the second way is best for practice. Because when we reading code, we want to know what purpose the code. So, if the code goes first then it's more clear and understandable for the reader and knows what is the purpose of the functions.

// the code which uses the functions 
let obj = createObject();
setObject(obj);
// --- helper functions ---
function createObject() {
//your code here
}
function setObject(obj) {
//your code here
}

Comments

We all know about comments. A comment could be a single line ‘//’ or multiple lines ‘/*…….*/’. We use comments in code to describe how and why code works. Sometimes we use comments in the wrong way in code like this —

// This code will do this thing (...) and that thing (...) 
// ...and who knows what else...
very complex code;

If we replace comments with self-descriptive code then it will more attractive. With self-descriptive code, we can understand code easily. like this —

function showPrimes(n) {      for (let i = 2; i < n; i++) {     
if (!isPrime(i)) continue;
alert(i);
}
}
function isPrime(n) { for (let i = 2; i < n; i++) {
if (n % i == 0) return false;
}
return true;
}

we all know this about the error. Now I tell you how to catch errors in code. It’s very simple to catch errors in code. Just try and catch 😄. Sounds funny right?

try….catch

You can handle errors by using try..catch block. “To err is human.”- So programmers can do mistakes in code.

try {

//your code
}catch(err) {//error handle}

You should know that try..catch block works in runtime. If there are some errors in your code and for this reason, your code could not compile then try..catch block was not worked.

try..catch..finaly

if you want a part of code that always executes then will use try..catch..finally block. finally block is always executes if there any error occurs or not in the runtime.

try {

//your code
} catch(err) {
//your error handle code
} finally {
//your code here}