JS areas of exploration
Published: 2021-12-20 11:33:38


**Scope:**
Scope is the accessibility of variables, functions, and objects in some particular part of your code during runtime. In other words, scope determines the visibility of variables and other resources in areas of your code.
As per the above definition of Scope, So, the point in limiting the visibility of variables and not having everything available everywhere in your code.
the scope is defined in the main two ways,

-Global Scope
-Local Scope
var greeting='Welcome to blog';
(function(){
 console.log(greeting); //Output: Welcome to blog
})();
consider above code greeting variable should be global scope, it can access inside the function,
(function(){
var greeting = 'Welcome to blog';
 console.log(greeting); //Output: Welcome to blog
})();
console.log(greeting); //Output:Reference-Error greeting not defined
In the above code for local scope,


**Memoization:**
Memoization is a programming technique that attempts to increase a function’s performance by caching its previously computed results. Because JavaScript objects behave like associative arrays, they are ideal candidates to act as caches. Each time a memoized function is called, its parameters are used to index the cache. If the data is present, then it can be returned, without executing the entire function. However, if the data is not cached, then the function is executed, and the result is added to the cache.
The function is an integral part of the programming, they help to modularity and reusable to our code, as per above definition memoization is an optimizing our code
const memoizedAdd = () => {
       let cache = {};
       return (value) => {
           if (value in cache) {
               console.log('Fetching from cache');
               return cache[value];
           } else {
               console.log('Calculating result');
               let result = value + 10;
               cache[value] = result;
               return result;
           }
       }
   }
// returned function from memoizedAdd
const newAdd = memoizedAdd();
console.log(newAdd(9)); //output: 19 calculated
console.log(newAdd(9)); //output: 19 cached
from the above code, you can understand memoization.


**Async & Await:**
Babel now supporting async/await out of the box, and ES2016 (or ES7) just around the corner, async & await basically just syntactic sugar on top of Promises, these two keywords alone should make writing asynchronous code in Node much more bearable.
However, what a lot of people may have missed is that the entire foundation for async/await is promises. In fact, every async function you write will return a promise, and every single thing you await will ordinarily be a promise.
async function getUserDetail() {
   try {
       let users = await getUsers();
       return users[0].name;
   } catch (err) {
       return {
           name: 'default user'
       };
   }
}
Above code for a simple async & await,
Good, async/await is a great syntactic improvement for both nodejs and browser programmers. Comparing to Promises, it is a shortcut to reach the same destination. It helps the developer to implement functional programming in JavaScript, and increases the code readability, making JavaScript more enjoyable.

**Callback function:**
A reference to executable code, or a piece of executable code, that is passed as an argument to other code.
From the above definition, the callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
function greeting(name) {
console.log('Hello ' + name);
}
function processUserInput(callback) {
   //var name = prompt('Please enter your name.');
   name = 'chisty';
   callback(name);
}
processUserInput(greeting); //output Hello chisty
In this above program, function greeting passed as an argument to the processUserInput function, so I hope you now understood callback function in JavaScript.

**Promise:**
The Promise object represents the eventual completion (or failure) of an asynchronous operation, and its resulting value.
A promise represents the result of the asynchronous function. Promises can be used to avoid chaining callbacks. In JavaScript,

**Polymorphism:**
Polymorphism is one of the tenets of Object-Oriented Programming (OOP). It is the practice of designing objects to share behaviors and to be able to override shared behaviors with specific ones. Polymorphism takes advantage of inheritance in order to make this happen.
let's look at the sample code for an override a function in JavaScript
var employee = new Employee('chisty');
//override function
//this method going to execute
Employee.prototype.getDetails = function() {
   return this.name.toUpperCase();
}
console.log(employee.getDetails()); //outPut: CHISTY
//object and prototype function
function Employee(name) {
   this.name = name;
}
Employee.prototype.getDetails = function() {
   return this.name;
}
In the above simple program prototype-based method for an Employee constructor function has to override by another prototype function as return the Name as uppercase.