10 JavaScript ES6 related things that we should know

Md Asifuzzaman Suvo
4 min readNov 3, 2020

Block Binding

The variable declaration is a tricky things in all programming language.in most other language variable are created at spot where the declared variable are occurred. In JavaScript variable are actually created depends on how we declare them .And then ES6 make it easier to us

Var Declarations and Hoisting

Var declaration is acted like if they are hoisted to the top of the function or the global scope ,and they act like if they are outside of a function

Example :

function newSection(condition){

var food

if(condition){

name=’Burger’

console.log(name)

}

else{

here we see that the function called in the start point of the code.but we can access the name variable from everywhere. ES6 introduces us clock-level declarations like let and const

Block-Level Declarations

Block level declaration are occurred in two steps ,they are :

  1. Inside of a function
    2.Inside of a block

let Declarations
The let declaration syntax is the same as the syntax for var. You can basically replace var with let to declare a variable, but the limit is only the current code block.

Example:

function getRoll(condition) {

if (condition) {
let value = “eleven”;

// other code

return value;
} else {

// value doesn’t exist here

return null;
}

// value doesn’t exist here
}

const Declarations

when we declared a variable with const .then we cant change this value the second time thats means this is value is never be change in future ..like it is constant value.

Example:

const pi = 3.14;

pi = 5; // error

this thing is make a error .because the const is never changed .

Block Binding in Loops

for (var i=0; i < 10; i++) {
process(items[i]);
}

// i is still accessible here
console.log(i);

here i is accessible here because, In JavaScript, the variable i is still accessible after the loop is completed because the var declaration gets hoisted.

for (let i=0; i < 10; i++) {
process(items[i]);
}

// i is not accessible here - throws an error
console.log(i);

In this example, the variable i only exists within the for loop. Once the loop is complete, the variable is destroyed and is no longer accessible elsewhere.

Emerging Best Practices for Block Bindings

Before the ES6 development there was belief you should use let by default instead of var for variable declarations in JavaScript. when the ES6 const were arrive in JS the developer thing that is needed for modification protection

Then the developers are willing to use let and const .and not use the var to declared the variable .because they should not change their value after initialize them .This idea has a significant amount of traction and is worth exploring in your code as you adopt ECMAScript 6.

Default function parameters

this type of parameter is named with a default value ..if there is no value then the value is undefined

Example:

function multiply(a, b = 6) {
return a * b;
}

console.log(multiply(5, 6));

console.log(multiply(9));

OutPut: 30 and 54

JavaScript Spread Operator

var newValue = [...value];

here the syntax operator is “…”

this is a syntax operatot .that means it will target the all value of previous “value”, When … occurs in function call or alike, its called a spread operator. Spread operator can be used in many cases, like when we want to expand, copy, concat, with math object

Arrow Function

Arrow function is a compact alternative to a normal function, but it is limited and can’t be used in all situations.it does not have any arguments,or new.target keyword.it can not be used as constructor

Example:

hello = () => {
return “Hello World!”;
}

Coding Style

The coding style should be depend on the script of code .there are coding style that we must follow — — — —

Curly Brace are not needed ,beginners sometime do that

Line length is more important to white some code ,because no one like to read big horizontal code .you have to split it to user friendly

use linters for better code expression .

Comments

An important sign of a good developer is comments: their presence and even their absence.

Good comments allow us to maintain the code well, come back to it after a delay and use it more effectively.

When we comment on any code — — —

When there is usage some function and define the overall structure ,to give up the main high level view we can comment out …and also when an important solution occurs then we should comment out some code

When we dont comment to any code — — -

To give a description like how the code works and how does the code is ,we try to not comment out in these types of codes

--

--