10 Quick Concepts To Become A Better Javascript Programmer

Md. Faizur Rahman Khan
4 min readNov 3, 2020
Concepts To Become A Better Programmer

Primitive Data Type vs Non-primitive Data Type

Primitive Data Type:

Primitive data are single values. They have no additional properties and methods. Boolean, byte, short, int, long, float, double, char are called primitive data types.

Examples:

console.log(2); // 2
console.log("hello"); // hello
console.log(undefined); // undefined
Primitive Data Types

Non-primitive Data Type (Objects and Functions):

A non-primitive data type is like an array. Structure, object, functions, or class are known as non-primitive data types. These are created by the programmers. These are not defined by the programming language.

Example:

const person = {
name : "faizur rahman",
age: 17;
}
person.age = 25;
console.log (person.age); // 25.
Non-primitive Data Types

Javascript Error Handling

Syntax Error:

This represents an error in the syntax of the code. When we try to interpret the syntactically invalid code, the error occurs. When the JavaScript debugger encounters tokens or token order that does not refer to the syntax when the code is parsed, the syntax error is thrown.

let x=2;
consol.log(x);

Runtime Error:

Runtime errors also called exceptions. These occur during execution after compilation or interpretation.

For example, the following lines cause a runtime error. Here the syntax is correct. But at the debugging time, it is trying to call a method that does not exist.

Example:

<script>   
window.printme();
</script>

Logical Error:

Logic errors are the most difficult type of errors to track down. These errors are not the result of a syntax or runtime error. Instead, they occur for the fault in the logic that drives your script.

You cannot catch those errors, because it depends on your business requirement and what type of logic you want to put in your programs.

The try…catch…finally Statement:

  • The try statement is used to test a block of code for errors.
  • The catch statement is used to handle the error.
  • The throw statement is used to create custom errors by the programmer.
  • The finally statement is used to execute code, after try and catch regardless of the result.

Example:

<script>

function myFunc() {
var a = 100;

try {
alert("Value of variable a is : " + a );
}
catch ( e ) {
alert("Error: " + e.description );
}
finally {
alert("Finally block will always execute!" );
}
}

</script>

ES6 Fundamentals

Spread Operator (…) :

The spread operator is a very special and useful operator. It helps to copy all the values from an array or object. It combines any array into one combination. It is used (…) before that variable.

Example:

function sum(a,b,c) {
return a+b+c;
}
const result = [78, 89, 95];
const totalResult = sum (...result);
console.log (totalResult); // 262

Arrow function (=>) :

It is one of the best features of ES6. It helps the coder to write a short function in an efficient way. Now see an example of normal function vs Arrow function below.

Example:

//Normal Function Declarationlet y= function (x, y) {
return x + y;
}
//Arrow Function Declarationlet y = (x, y) => x + y;

Array Destructuring [ … ]:

It is one of the coolest features of ES6. It is mostly used for the destruction of an array and object. In array destructuring we use []. Inside of this square bracket, we put variables that refer to the value of this array.

Example:

const names = [ "Faizur", "Sharif", "Faruk" ];
const [firstName, secondName, thirdName] = names;
console.log (firstName, secondName, thirdName); // Faizur Sharif Faruk

Promise:

The Promise object represents the final completion (or failure) of an asynchronous operation and its resulting value.

The promise has three states. They are given below:

  • Pending: Initial state, neither fulfilled nor rejected.
  • Fulfilled: Meaning that the operation is completed successfully.
  • Rejected: Meaning that the operation is failed.

Example:

const giveBorrow = (myBalance, askingAmount ) => {
return new Promise((resolve, reject) => {
if(myBalance >= askingAmount) {
resolve("Here your money!");
}
else{
reject("Sorry, don't have enough money!");
}
})
}

Coding Style:

As a programmer, we should be careful about our coding style. So that anyone can see our code and quickly understand what is happening in our code. We should not just write the code the way we understand. Rather we should code in such a way that all programmers and anyone can understand our code with ease. That’s why we should keep the code as clean and simple as we can. We should also try to use semicolons, commas in our code where it needs to be. It’s a better coding habit.

Example:

function factorial (n) {
var factorial = 1;
for (var i = 1; i <= n; i++) {
factorial = factorial * i;
}
return factorial;
}
var result = factorial (5);
console.log (result); // 120

--

--