Modern JavaScript You Should Know

Comments
Comments help us understand that what is going on here. In our code, we use single-line comments or multi-line comments. It is similar to C / C++ programming language. For single-line, we use forward slash // and for multi-line /*…*/. For example —
DataType
JavaScript variables contain many data types such as numbers, strings, and objects etc. In general, there are two types of data type 1. Primitive and 2. Nonprimitive, Primitive holds integers, floats, and strings. On the other hand, Non-primitive means Object and function types data. For example –
primitive data type
Object type data
const array = [ 10, 20, 30, 40 ];
const objects = { name: ‘sabbir’, dept: ‘cse’ };
there is another way to use a String object
Now the interesting part is a function type, print the below code to see what’s happened...
Error Handling in JavaScript
We make mistakes intentionally or unintentionally in our coding life. We fix it as a bug from our application. It commonly happens for our coding or server-side loading problem. To avoid error, we use try .. catch formula. For example –
In catch, err argument has some property such as name, message and stack.
Function in JavaScript
Function concept is so important for a programming language. JavaScript has some types of functions. We use the function to reduce code, stop repetition same type of code and organized our code.
the general shape of a function —
function sum(n) {
return n*n;
}
sum(4);
Functions with Default Parameter Values
The above function I have passed a value 4 and function also accepts this value. Noticeable that, sum function has only one parameter but what will happen if you don’t pass a value? Definitely, you will not get any result from there.
But, you can set a default value if parameters are absent then the default value will assign automatically. Unintentionally errors will be handled.
For example –
But In modern JavaScript, there are some other default function. We can use conditional (ternary) operator –
We can direct assign value with parameters
Working with Unnamed Parameters
JavaScript also supports multiple parameters at the same time. Commonly, it is called arguments, it returns an object.
function doSum() {
console.log(arguments)
}
const addition = doSum(5, 10, 12);
we can use it through iteration
Rest Parameters in JavaScript
Rest parameters concept is now a hot topic of modern JavaScript. Three dots are used for rest parameters and seats at the last position. You may use some other parameters but those are seat before rest parameters. For example —
The Spread Operator
The spread syntax can be used to merge arrays or copy arrays. It also uses three dots to copy an array or object. For example –
Arrow function
Arrow function is simple and easy to use. It takes an argument as a normal JS function and an arrow is used before curly braces.
Anonymous function
There is another useful function which has no function name is called anonymous function. For example –
Block-Level Functions
A function that is in a block scope, can be declared from the block, and can be hoisted upper scope is called block-level function.
But using let/const for a block-level function doesn’t support hoisting and block scope is considered for let/const characteristic.