Some important things for JS programmer

Tanjid Mahamud
4 min readMay 6, 2021

1. Cross Browsing Testing :

In the world, there have many people who use internet on different browser. Some of them like updated new browser and some of them like to use old version of browser. Cross browsing testing is the practice to make sure that web-site or web browser is working different browsers. It is very essential for a web developer to make his website or web application make cross browser testing. Because all of users of the web site or web application don’t use the same browser. So it is very important.

2. Comments in Programming Language :

To better understand code writing comments is very important thing. But most of us do a mistake when we write any comment. Basically novices try to explain “What is going on in the code ”, but it is totally wrong way to use comments. Comments are not writing for it. Comments write to describe the architecture, function’s parameters and it uses.

3. Error handling with “try….catch”

As a programmer it is very important to keep knowledge about “try…catch”. For Error handling try…catch is used.

Syntax :

try {

// code…

} catch (err) {

// error handling

}

It works like this:

1. First, the code in try {…} is executed.

2. If there were no errors, then catch (err) is ignored: the execution reaches the end of try and goes on, skipping catch.

3. If an error occurs, then the try execution is stopped, and control flows to the beginning of catch (err). The err variable (we can use any name for it) will contain an error object with details about what happened.

4.Var Declaration and Hoisting :

Variable declaration using var is counted as if it declare at the of the function and regardless of where the actual declaration occurs; this is called hoisting.

Example :

<script>

function getColor(color) {

if (color) {

var value = “blue”;

// other code

return value;

} else {

// value exists here with a value of undefined

return null;

}

// value exists here with a value of undefined

}

</script>

5.Block Level Declaration :

Block level declaration means those variables which are not accessible out side of a given block. Block scopes are created in two ways

1. Inside of a function

2. Inside of a block as { and } characters

6. Let Declaration :

let declaration is same syntax as for var. You can basically replace var with let to declare a variable, but difference is that let is used for only block level declaration.

EXAMPLE :

<script>

function getColor(color) {

if (color) {

let value = “blue”;

// other code

return value;

} else {

// value doesn’t exist here with a value of undefined

return null;

}

// value doesn’t exist here with a value of undefined

}

</script>

7. const Declaration :

const declaration is same syntax as for var. You can basically replace var with const to declare a variable, but difference is that const is used for only block level declaration.

EXAMPLE :

<script>

function getColor(color) {

if (color) {

const value = “blue”;

// other code

return value;

} else {

// value doesn’t exist here with a value of undefined

return null;

}

// value doesn’t exist here with a value of undefined

}

</script>

8. let vs const :

To declare variable let and const are almost same but has an one big difference. And the difference is that using const variable’s value cannot be changed.

EXAMPLE :

<script>

let num1 = 23;

num1 = 25;

console.log(num1) //In output it will show 25.

</script>

Again,

<script>

const num1 = 23;

num1 = 25;// It will show an error. Because using const variable’s value cannot be changed

</script>

9. Block Binding loops

for (var i=0; i < 10; i++) {process(items[i]);}// i is still accessible hereconsole.log(i);                     // 10

In this case, if we declare i as var, it will be available after the for loop. And which can be very obnoxious for a programmer. To reduce this irritation we can use let.

EXAMPLE :

for (let i=0; i < 10; i++) {process(items[i]);}console.log(i); // i is not accessible here & show an error

9. Coding Style :

When a Programmer write any kind of program it is very essential to focused on his coding style. Because without coding style, program may work but when he re-read or other programmer re-read codes he can not understand. Again if Codes are not well styled, it is very difficult to find out bug. So coding style is very essential for a programmer.

10 . Functions in Loop :

var array= [];for (var i=0; i < 10; i++) {array.push(function() { console.log(i); });}array.forEach(function(array) {array();     // outputs the number "10" ten times});

On this code, we expect that output will show 0 to 9. But unfortunately outputs the number ‘10’ ten times.

To reduce this problem we can use immediately-invoked expressions inside of loops to force a new copy of the variable they want to iterate over to be created.

EXAMPLE :

var array= [];for (var i=0; i < 10; i++) {array.push((function(value) {return function() {console.log(value);}}(i)));}array.forEach(function(func) {array ();     // outputs 0, then 1, then 2, up to 9})

--

--