Fundamental Rules of JavaScript

Nasir Uddin
5 min readMay 6, 2021

Try & Catch

Try blocks executes are define value and show the success result. catch block getting error and show that error and show in console. So try and catch are two methods likely Synchronously and Asynchronously… Synchronous methods are distributed as normal execution. Most probably used in the javaScript asynchronous method for single-threaded any await systems.

Catch are getting all errors. If we don’t know how to handle error to throw new error.message and error.name and error.code

Example Try and Catch used with synchronous .

let alarm = 5;

try {

console.log(alarm);

} catch (error) {

console.log(error);

}

// Synchronously

try {

setTimeout(() => {

values; // this variable not defined. this error are send catch block

}, 1000);

} catch (err) {

console.log(err.name); // show error in console and take error catch block

}

console.log(“Getting Synchronous function”);

So we can say javaScript is single threated non-blocking programming language

setTimeout(() => {

try {

getError;

} catch (error) {

console.log(error.name);

console.log(error.code); // showing undefined

console.log(error.message);

}

}, 0);

console.log(“Now showing error”);

Coding Style

Coding style is most important for a developer. when use function takes function name before taking 1 space and take parameter there is no space with function name and parentheses but parameters values need between a gap and use comma for separation. use semicolon for ending statement use around gap for operators.

Example Below:

function hello(n, m) {

//take function name and don’t take any space before parameter

let take = n + m; //finally use statement like semicolon;

if (take) {

//their is 1 up space for using statement

console.log(take);

}

}

hello(5, 6); // call the function take 1 upper space

Comments

A comment is most important for every single coding. We have to need use comments in the proper way. There are two types of comments 1. Bad Comment & 2. Good Comment

1.Bad Comment

function multiply(n) {

// there is to do multiply

// this is a single value

// this is not a string

return n * 2;

}

2. Good Comment

A good comment uses a multiline comment option when more line comment and comment level is lower so use single line comment style.

function add(n, m = 3) {

/*there is to do multiply

this is a single value

this is not string*/

return n + m;

}

Cross Browser

  • If you make the web work for everyone provides a more useful perspective on the different browsers people use, their market share, and related cross-browser compatibility issues, and uses your application on Mozilla, opera, and internet Explorer so you can use -WebKit in your CSS file on HTML body tag.
  • Example Below:

body {

background: url(./image.jpg) no-repeat center center;

-moz-background: cover; // Responsive for mozilla

-o-background: cover; // Responsive for opera

-i-background: cover; // Responsive for internet explorer

background: cover;

}

Block Bindings

  • Block binding is the traditionally tricky part of programming language. JavaScript is a C-based programming language. Where you created a variable declaration on your declare them and ECMAScript offers the option to make a consideration and control easier.

Var Declarations

  1. When var declaration var are treated as they are the top of the function and var is global scope variable & you can use and declare var outside of the function.
  2. Example Below:

function value(statement) {

if (statement) {

var color = “blue”;

return color;

} else {

// color exists here with a value of undefined

return null;

}

// color exists here with a value of undefined

}

  1. JavaScript excepted the variable value to only created if condition evaluates the variable value is created regardless and behind the scenes javaScript engine change the function looks
  2. Example below:

function value(statement) {

var color;

if (statement) {

color = “blue”;

return color;

} else {

// color exists here with a value of undefined

return null;

}

// color exists here with a value of undefined

}

Let Declaration

let declaration same as the var declarations. such as change the var value and change the let value. So we can let and var syntax as the same definition but some problem is created var declarations because var is leak variable like as global type variables but let is scope variable it’s not used outside of functions or any block but change the let assigned value.

Example Below:

function value(statement) {

let color;

if (statement) {

color = “blue”;

return color;

} else {

// color exists here with a value of undefined

return null;

}

// color exists here with a value of undefined

}

Constant Declarations

  • If you want to define a variable in ECMAScript const declaration syntax. This variable when you initialize the first time that value is you set you can not change the value. Every const variable must be first initialized needed otherwise show syntax error and missing initialization.
  • Example Below:

const fruits = “apple”;

fruits = “orange”; // now you are getting error

const food; // now showing missing initialization

Declaring Objects with Const

  • When a const variable is declared itself prevent modification on const value. That means const variables with objects do not prevent modifications.
  • Example Below:

const player = {

name: “Musfiqe”,

age: 35,

};

(player.name = “Shakib”), // This code modification is working

(player.age = 34); // This code modification is working

player = {

// now throw error this code modification

name: “Mustafiz”,

age: “28”,

};

Block Binding in Loops

  • when you used var variable in your loop blocks your can use your counter variable outside your loop blocks. Because var declaration is leak systems.
  • Example Below:

var array = [2, 3, 4, 5, 6];

for (var i = 0; i < array.length; i++) {

var item = array[i];

}

console.log(item);

  • If you are used to Let variable for your loop blocks you can not use your counter or declaration variable outside of your loops blocks.
  • Example Below:

const array = [3, 4, 5, 63, 6];

for (let i = 0; i < array.length; i++) {

const element = array[i];

console.log(element); // working here

}

console.log(element); // throw error

Arrow Function in ECMAScript

  • Arrow function is a compact alternative traditional function. There is a limited situation for using some keywords. Likely:
  1. Arrow function hasn’t binding to this.,super, method should not be used here.
  2. Arrow function hasn’t arguments, new. target keywords
  3. Arrow function not suitable for use call, apply and bind methods generally it’s used for a scop.
  4. Arrow function can’t be used constructor methods.
  • Example Below:

const arrowFunction = (arr) => {

return arr.length;

};

const array = [1, 2, 4, 5, 6, 4, 3, 5, 4, 6, 4, 3];

console.log(arrowFunction(array));

--

--