JavaScript Fundamental Concept Data type, Block, and Function.

HM YOUNUS
3 min readNov 3, 2020

1: Data Type;

There are basically two types of data types in JavaScript:

(1) Primitive data type (2) Non-Primitive Data type:

(1) Primitive data type:

Values ​​are stored directly in primitive type data. We know all objects in JavaScript. But without these primitive data types. These are not objects. As like: Number, String, Boolean, Undefined, Null.

(2) Non-Primitive Data type:

The values ​​of the non-primitive data types are not saved directly. Rather the value reference remains saved. And this type of data object. This means that they also have a lot of properties. as Like: Array, Object, Function.

2: Comment:

Commenting on code is a good practice. Because it is often difficult to understand which part of the code is doing what. In that case, if you write down some description that this part works, that part works then anyone or you can see your code after a few years and understand that this part is actually working. So it is better to keep proper comments in the code. And the comments will be completely avoided by the engine of JavaScript. This means they will never come to the output

Comments can be made in two ways:

1: Single line comment: If you want to write something at the end of your code if you comment on the same line

const myName = 'md younus'; // your comment here

2: Multi-line comment: If you want to write a comment in a few lines:

/* Your Comments here*/

3: Block binding:

Variable means binding. we declare a variable, we bind a value to a name inside a scope. confusion in binding before ES6.Because block binding was not introduced then. Javascript hoisted everything to the top rather we declare a variable within a block. we can access this from any other block. To remove this confusion/problem, Javascript introduces Block binding in ES6.

4. Var Declarations and Hoisting:

variable declarations use the var keyword.it is treated that variable is declared at the top. where the actual declaration occurs. This is called hoisting. example:

function getValue (condition) {if (condition) {var name = “MD YOUNUS”;return name;} else {return null;}
}

5. Block-Level Declarations:

variable declare anything block-level.inaccessible from the outside the given block. Block scopes are created:

  • Inside of a function
  1. Inside of a block ( indicated by {} curly brackets)

6. Global Block Bindings:

Let and the const keyword is instead of var when we declare any variable globally. Because var key overwrites our window variable that is already declared in our global object. But let and const keyword doesn’t overwrite the existing global variable, make a shadow variable of its. So this is the best practice to use the let or const keyword when we declaring anything globally.

7. Block Binding in Loops:

var variable is also problematic when it is declared in loops. Because outside the loop block, they are accessible. Because the var declaration gets hoisted. Example:

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

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

8. Emerging Best practices for Block Bindings:

The use of const is safe when we declaring any variable globally if our variable doesn’t need any modification in the future. Because unexpected value changes are a source of bugs. If we need modification in the declaration, we can use let which is safer than var. Because var has to scope.

9. Function With Default Parameter value:

If we want to set a default value of a given parameter when defining a function, then we can easily use this default parameter of ES6. However, the matter was not so easy in ES5. examples.

function makeRequest(url, timeout, callback) {

timeout = timeout || 2000;
callback = callback || function() {};

// the rest of the function

}

10. The Spread Operator:

The spread operator is a useful and quick syntax for adding items to arrays, combining arrays or objects, and spreading an array out into a function’s arguments.

The spread operator was added to JavaScript in ES6 (ES2015), just like the rest parameters, which have the same syntax: three magic dots .

let values = [25, 50, 75, 100]

// equivalent to
// console.log(Math.max(25, 50, 75, 100));console.log(Math.max(...values)); // 100

--

--

HM YOUNUS
0 Followers

🚀 Passionate Full-Stack MERN Web Developer | ☁️ DevOps Cloud Specialist | 🌟 Transforming Ideas into Digital Reality .