#JavaScript 2019#New in ES
JavaScript-2019 added so many new in build functionality which are very helpful. In this article I am going to discuss those functionality and new features.
Class
Add new symbol to define private variables in class(#)
Example for simple variable and function
class Scipt2019Class {
user_name ="Biplab Malakar"; // public field
#user_phone_number = "9592350924"; //private variable
constructor() { }
getName() { //public function
return this.user_name;
}
#get Age() { // private function
return this.#user_phone_number;
}
}
Example for static variable and static function
class StaticClass{
static #designation = 'Software Developer"; // static variable
static getDesignation(){
return StaticClass.#designation;
}
}
New trim function.
Functionality as trim with new features
const first_name = " Biplab ";
const last_name ="Malakar ";
const full_name = first_name.trimStart() + last_name.trimEnd();
console.log('Full name is: ', full_name);
// trimStart() trim form the beginning only
// trimEnd() trim from end only
Big Integer
In javascript to define big integer we used Number.MAX_SAFE_INTEGER(2^53).Now we can use BigInt() to define large number which is larger than current maximum value.
//'n' syntax to declare BigInt
const big_number_1 = 9100000000000001n;
//use BigInt() constructor
const big_number_2 = BigInt(9100000000000002);
//BigInt() constructor with string
const big_number_2 = BigInt('9100000000000002');
Array Functions
Before 2019 new function we use our own login to make dimensional array form multi-dimensional array. Now JavaScript provide flat() and flatMap() to generate single dimensional array.
//flat()
const array = [1,[2,3],[4,5,[6,7,[8,9]]]];
array.flat(3); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
// flat(n); n is the depth by default depth is 1
array.flat();//[1, 2, 3, 4, 5, [6,7,[8,9]]]; //it extend upto depth 1
array.flat(2) // [1,2,3,4,5,6,7,[8,9]]; // it extend upto depth 2
//flatMap() same as map() but instead of return nested array it will return one-dimensional array
const sentence = ['I am', 'Biplab Malakar'];
let step_1 = sentence.map(d=> d.split(' '));// [['I', 'am'], ['Biplab', 'Malakar']]
let step_2 = step_1.flatMap(d=> d);// ['I', 'am', 'Biplab', 'Malakar']
Object Creation from array
We can create object from one-dimensional array
const obj = Object.assign({}, [1,2,3,4,5]); // {0: 1, 1: 2, 2: 3, 3: 4, 4: 5}
// if it's tow-dimensional array [['a', 2], ['b',4], ['c', 6]] and
//I want object {a: 2, b: 4, c: 6}
const obj_2 = Object.fromEntries( [['a', 2], ['b',4], ['c', 6]]); // {a: 2, b: 4, c: 6}
Apart from those so many changes are proposed like