JavaScript is a single-threaded non-blocking asynchronous language. It works asynchronously.

Example:

console.log("Hello World!")setTimeOut(() => {
console.log("I am Emon")
}, 1000)
console.log("A web developer")// Output is:
Hello World
A web developer
I am Emon

primitive value can be a number, string, boolean, symbol, and other things.All primitives are constant, they cannot be altered.

console.log(5);
console.log('hello world');
console.log(null);
console.log(undefine);

OPP means object-oriented programming. It’s called syntactic sugar.

class Person{
constructor(firstName, lastName){
this.firstName = firstName;
this.lastName = lastName;
}

getFullName() {
return this.firstName + " " + this.lastName;
}
}
const person1 = new Person('Emon' , 'Hossain');
console.log(person1)
// output is : Person {firstName: "Emon", lastName: "Hossain"};

Introduising About ES6

ECMAScript 2015 or ES2015 is a significant update to the JavaScript programming language. It is the first major update to the language since ES5 which was standardized in 2009. Therefore, ES2015 is often called ES6.

Block Bindings

In JavaScript, variable declare is an important factor. In a variable declaration, it depends on which methods you will declare a variable and where it will be. ES6 provides a new way to declare so that you can more easily control the scope of variables.

Var Declarations and Hoisting

Variable declarations using var are treated as if they are at the top of the function (or global scope, if declared outside of a function) regardless of where the actual declaration occurs; this is called hoisting. Example-

function getName(condition) {    if (condition) {       var name;       // other code
name= "Emon";
return name;
} else {
// name doesn’t exist here
return null;
}
// name doesn’t exist here
}

in the example, the variable is declared to the inside of the if condition. so that the name variable is not accessible on the else condition and outside of the condition.

function getName(condition) {

var name;

if (condition) {
name= "Emon";

// other code

return name;
} else {

return null;
}
}

The declaration of name variable is hoisted to the top. That means the variable name is accessible from the else condition. If accessed from there, the variable would just have a value of undefine because it hasn't been initialized.

Block-Level Declarations

Block-level declarations means the declare variable are not accessible outside the block scope Block scopes are created:

  1. Inside of a function
  2. Inside of a block (indicated by the { and } characters)

Let Declarations

The let and var declaration syntax is same. You can use let in place of declaring var. And if you do this you have to remember that the scope of the variable will remain into the current block because var is hoisted on the top of the function but let is not hoisted.

Example-

function getName(condition) {if (condition) {let name;        // other code
name= "Emon";
return name;
} else {
// name doesn’t exist here
return null;
}
// name doesn’t exist here
}

Const Declarations

If we declared a variable with the const variable, we can’t change this value. The value is constant.

const age = 20;
maxItems = 21 ; // throws error

In the example, the age not be changed because of its a constant value.

Block Binding in Loops:

In JavaScript, most often case developers want block-level scoping of variables is within for loops. For for this we have to use let not var, cause var is being hoisted. Follow the two examples below:

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

Here, outside the loop, i is accessible because we use var

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

React Developer || JavaScript Developer || MERN Stack