DEV Community

Cover image for Advanced JavaScript Concepts | Revised Version for Jr Dev's
Syed Muhammad Ali Raza
Syed Muhammad Ali Raza

Posted on

Advanced JavaScript Concepts | Revised Version for Jr Dev's

JavaScript Revised Version

1. let, var, and const

var

In the early days of JavaScript, the var keyword was used to declare variables. However, it had a few issues, especially associated with scope. Variables declared with var are function-scoped, meaning they may be seen at some stage in the function in which they're declared, even earlier than they may be actually initialized. This ends in unpredictable consequences because of hoisting.

characteristic test() 
  console.Log(x); // undefined, due to hoisting
  var x = 10;

Enter fullscreen mode Exit fullscreen mode

let

To deal with var's issues, ES6 added permit, which presents block-level scope. This approach variables declared with let are handiest available inside the block `` in which they are declared.

`js
characteristic take a look at()
console.Log(x); // ReferenceError, as x isn't described
allow x = 10;

`

const

const is likewise block-scoped like permit however is used to declare constants. Once assigned a fee, you cannot reassign a variable declared with const. However, const does now not make the variable's value immutable (in particular with objects).

`js
const arr = [1, 2, 3];
arr.Push(four); // Works
arr = [5, 6]; // TypeError: Assignment to consistent variable
`

Image description

2. Closures

A closure is created while a function "remembers" the variables from its outer scope, even after that scope has exited. Closures are important while managing features internal different features, where the internal function keeps access to the variables of the outer function.

`js
function outer()
let depend = zero;
return function inner()
remember++;
console.Log(depend);
;

const counter = outer();
counter(); // 1
counter(); // 2
`

In the instance, the internal characteristic counter retains get entry to to the be counted variable even after the outer function has finished.

Image description

3. Currying

Currying is a way wherein a characteristic takes a couple of arguments one after the other, returning a new function for each argument. It transforms a feature that takes all arguments right now into one that takes them sequentially.

`js
function multiply(a)
go back characteristic(b)
go back a * b;
;

const double = multiply(2);
console.Log(double(five)); // 10
`

This method may be beneficial in useful programming wherein partial utility of features is commonplace.

Image description

4. Promises

Promises are used to handle asynchronous operations. A promise represents an operation that hasn't completed but but is anticipated to inside the future. It can be in one in all 3 states:

  1. Pending: Initial country, neither fulfilled nor rejected.
  2. Fulfilled: Operation finished successfully.
  3. Rejected: Operation failed.

`js
const myPromise = new Promise((resolve, reject) =>
setTimeout(() =>
clear up("Success!");
, one thousand);
);

myPromise
.Then((value) => console.Log(price)) // "Success!"
.Capture((errors) => console.Log(blunders));
`

Promises are extensively utilized in present day JavaScript to address async code in a cleanser way in comparison to callbacks.

5. Hoisting

Hoisting is JavaScript's default conduct of moving declarations to the top of their scope earlier than code execution. Only the declarations are hoisted, now not the initializations.

`js
console.Log(x); // undefined
var x = five;
`

In the case of var, the assertion is hoisted, but the fee venture takes place later. In evaluation, permit and const are not initialized till the code executes them, leading to a "temporal lifeless quarter" where they can't be accessed.

`js
console.Log(y); // ReferenceError: Cannot get entry to 'y' earlier than initialization
allow y = 10;
`

6. IIFE (Immediately Invoked Function Expression)

An IIFE is a characteristic that is finished straight away after it is described. IIFEs are useful for growing a private scope to avoid polluting the global scope, which was specifically essential before the arrival of modules.

`js
(feature()
console.Log("I am an IIFE");
)();
`

The feature is wrapped interior parentheses and is invoked at once after the remaining parentheses.

Image description

7. First-Class Functions (Citizen Functions)

In JavaScript, functions are great residents. This method that they can be assigned to variables, passed as arguments to other functions, and returned from other features.

`js
const greet = function(call)
return Hello, $call;
;

function sayHello(greetFunction, name)
console.Log(greetFunction(call));

sayHello(greet, "John"); // Hello, John
`

This assets is foundational to the practical programming style that JavaScript supports.

8. Variable Functions (Function Expressions)

In JavaScript, you may outline functions in two ways: the usage of function declarations and function expressions. A characteristic expression is while you assign a feature to a variable, permitting it to behave like every other variable.

`js
const upload = characteristic(a, b)
go back a + b;
;

console.Log(upload(2, three)); // 5
`

Function expressions allow for extra dynamic conduct in comparison to feature declarations.

9. Callbacks

A callback is a feature this is surpassed as a controversy to any other function and is achieved after a few operation is whole. Callbacks are closely utilized in JavaScript for asynchronous operations, which include dealing with user occasions or fetching information from a server.

`js
feature fetchData(callback)
setTimeout(() =>
callback("Data loaded");
, a thousand);

fetchData((statistics) =>
console.Log(facts); // "Data loaded"
);
`

With the advent of promises and async/look ahead to, callbacks are much less generally utilized in modern JavaScript for asynchronous tasks, but they may be nonetheless vital for certain styles.

Top comments (2)

Collapse
 
michal-so profile image
master029009

why we use IIFI?

Collapse
 
brysenrom profile image
Brysen-Rom

very informative