Some JavaScript Story

01. Coding Style

Coding Style not mandatory to apply for your code. But coding style is a good practice and your code clean and readable to apply code style.

Code style applies some rules:

  1. A space between parameters,
  2. Curly brackets { on the same line, after space,
  3. A use semicolon mandatory for style,
  4. A space after for/if/while,
  5. A space between arguments etc.

O2. Comments

We know comments code structure single line or multi-line. we normally use the describe how and why code works.

when using comments:

  1. Architecture, high level-view,
  2. Function usages,
  3. Important solutions etc.

when not use comments :

  1. How code works and what it das.

03. Var Declarations and Hositings

If we var declare to function top/outside of a function, that's the global scope /functional scope. Functionals cope variable use any position out of function or inside function.

Example:

function value1(condition) {

if (condition) {
var value = “blue”;

return value;
} else {

// value exists here with a value of undefined

return null;
}

// value exists here with a value of undefined
}.

04. Block Level Declarations.

Block-level declare to system/ way, 1.Inside of a function, 2.Inside of a block.

Let Declarations:

let declare system/syntax same as var system/syntax. basically, replace var with let to declare a variable, but the limit is only the current code block.

Example:

function value1(condition) {

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

// other code

return value;
} else {

// value doesn’t exist here

return null;
}

// value doesn’t exist here
}

Const Declarations:

const variables defined with const declarations syntax. variables declared using const their value can not be changed.

Example:

value constant
const terms = 30;

Syntax error: missing initialization
const name;

05. Block Bindings in Loops

This is one area where developers most want block-level scoping of variables is with loops, where the counter variable is meant to be used only inside the loop. For for this we have to use let not var, cause var is being hoisted.

Example:

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

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

06. Functions in Loops

The behavior of var made creating functions inside of loops problem because the loop variables are accessible from outside the scope of the loop.

var fun = [];

for (var i = 0; i < 10; i++) {
fun.push(function() { console.log(i); });
}

funcs.forEach(function(func) {
func(); outputs the number “10” ten times
});

07. Global Block Bindings

let and const are different from var is in their global scope behavior.

when var is used in the global scope, it creates a new global variable, which is a property of the global object. That means you can accidentally overwrite an existing global using var.

Example:

var RegExp = “Raju!”;

console.log(window.RegExp);

var abc = “Biswas”;

console.log(window.abc);

08. Arrow Function Expression

An arrow functions expression a compact alternative traditional function. but is limited can not be used in all situations. can not be used, constructor. Can not be used yield body. Not suitable for the call, apply bind method.

09. try……catch

try…catch is an error handling method.

try…catch syntax

try {

// code…

}

catch (err) {

// error handling

}

first code try{…} is execute if code no error catch ignore and if code error try ignore and catch in and showing error.

10. Spread Operator

spread operator array element and one more element adding create a new array easily also spread operator object element and another element adding e all element create a new object..and change original array or object easily.

Example: Array

function sum(x, y, z) {
return x + y + z;
}

const numbers = [1, 2, 3];

console.log(sum(…numbers));
output: 6

Example: OBJECT.

let p1 = {

name: “Raju”,

age: 25

}

let p2 = {

country: “Ban”,

occupation: “student”

}

let p = {…p1, …p2}

console.log(p);

output={

name: ‘Raju’,

age: 25,

country: ‘Ban’,

occupation: ‘student’

}

THANKYOU.

I'm a Front-end Developer and Familiar with work's Backend. I enjoy working with Team as a leader or member.I Always learning New technology. I enjoy it.