DEV Community

Nazmus Sakib
Nazmus Sakib

Posted on • Updated on

JavaScript Advanced: Explained in a Nutshell

JS Error

catch block is used to be executed when there is an error inside try block.
const x = 7;

try {
if(x < 5) throw "too low";
if(x > 10) throw "too high";
}
catch(err) {
console.log(err, 'x is ', x);
}

JS Arrow Function

In JS ES6 a function can be written in a shorter form.
const multiply = (x,y) => return x*y;

JS Classes

A JavaScript class is not an object, rather a template for JavaScript objects.
class Student {
constructor(name, roll) {
this.name = name;
this.roll = roll;
}
}

JS Callback

Callback is a function which is passed as an argument to a function. And it can be called inside that function in which function it was passed.
`function showResult(value) {
console.log(value);
}

function Summation(num1, num2, callback) {
let sum = num1 + num2;
callback(sum);
}

Summation(5, 5, showResult);`

JS Async Await

async makes a function return a Promise & await makes a function wait for a Promise.
const x= 2;
async const multiplication = () => {
for(i=1, i<10000000, i++) {
x = x*2;
}
multiplication().then(console.log(x));

Top comments (0)