JavaScript Some important topic.

Md Sujon Robin
3 min readMay 6, 2021

Var Declarations and Hoisting

Variable declarations of variables are considered to be at the top of the function (or global scope,
if declared outside of a function) wherever the actual declaration occurs. This is called hosting. examples below

function getName(condition) {

if (condition) {
var code = “pen”;

// other code

return code;
} else {
// code exists here with a value of undefined
return null;
}
// code exists here with a value of undefined
}

JavaScript engine changes the getName function. The declaration of code is hoisted to the top.
The variable code is actually still accessible from within the else clause.
If access from within the else clause the variable code will be undefined because it hasn’t been initialized, it's initialized only in the If clause.

function getName(condition) {

var value;

if (condition) {
code = “pen”;

// other code

return code;
} else {

return null;
}
}

Block-Level Declarations

1.Inside of a function
2.Inside of a block { \\code block }

The Let Declaration Syntax is the same as the variable Syntax.
the variable declaration with the original var but the limit is only the current code block.

function getName(condition) {

if (condition) {
let code = “pen”;

// other code

return code;
} else {

// code doesn’t exist here

return null;
}

// code doesn’t exist here
}

Block Binding in Loops

This is an area where developers want block-level scoping of most variables,
counter variables are only used inside loops.
For this, we do not have to use Let Very because the reason is being hoisted. Follow two Exam:

//Var used
for (var i = 0; i < 5; i++) {
process(items[i]);
}

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

//let used

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

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

Global Block Bindings

Another way to do that is to separate and let go.
When var is used in global scope which is a property of the global object. This means you can overwrite an existing global using Var

// in a browser
var RegExp = “var”;
console.log(window.RegExp); // “var!”

var ncz = “var one”;
console.log(window.ncz); // “var one”

If you use Let or Const instead for global scope, a new binding will be created in the global scope,
but no property is added to the global object. This means you can’t overwrite the global variable using Let or Cosnt, you can just shadow it.

// in a browser
let RegExp = “var”;
console.log(RegExp); // “var”
console.log(window.RegExp === RegExp); // false

const ncz = “var one”;
console.log(ncz); // “var one”
console.log(“ncz” in window); // false

Comments

Comments is two type .single line comment // and and multiline /* … */

Bad Comments

This is a bad comment.we will not use it.

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

Good Comments

This is a good comment. Such comments allow us to understand the purpose of the function and use it correctly without looking at its code.

/**
* This code will do this thing (…) and that thing (…)
*/
function naem(a, b) {

}

Function Default parameters.

These is default parameters. I do call function parameter 2 but I gave type 1 parameter value. Then work default value parameter.

function multiply(a, b = 5) {
return a + b;
}

console.log(multiply(5, 7));
// expected output: 12

console.log(multiply(5));
// expected output: 10

--

--