#JavaScript 2019#New in ES
Biplab Malakar
Posted on December 26, 2019
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}
reduceRight()
It's a new JavaScript(2019) array function. This function is same as reduce() function with the feature that it start evaluating from right to left.
const charArray = ['e','s','r','e','v','e','r'];
const word = charArray.reduce((ac, c)=> ac+c);
console.log('word is: ', word); // esrever
It starts evaluating from left and we get a string "esrever". Now if I want to evaluate this array from right so that my output is "reverse". This can be achieved by reduceRight()
const charArray = ['e','s','r','e','v','e','r'];
const word = charArray.reduceRight((ac, c)=> ac+c);
console.log('word is: ', word); // reverse
copyWithin()
This is also a new JavaScript(2019) array function. This function has feature to copy array element inside itself and output is reflected on original array. I know its confusing about what I am saying, lets look into example
const array = [1,2,3,4,5];
array.copyWithin(1);
console.log(array);
The output is [1,1,2,3,4]. array.copyWithin() this function copies your array elements and start place copy array from specified index. During the copy it will maintain the original size of the array. Consider the above example
- array.copyWithin(1), copy the all element of array and place this array from index 1.
- copy array is [1,2,3,4,5]. Original array size is 5. When it start placing element then it found that it extend the original size, so its ignore element 5.
const array = [1,2,3,4,5];
array.copyWithin(2);
console.log(array); // [1,2,1,2,3]
We can also define from which element it should start copy.
array.copyWithin(place_index, start_from_index);
array.copyWithin(1, 2);
- First Argument 1, denote copy array should be place from index 1.
- Second Argument 2, denote start copy elements from index 2. So copy items are 3,4,5
const array = [1,2,3,4,5];
array.copyWithin(1,2);
console.log(array); // [1, 3,4,5,5]
#5 prints two times because after 3,4,5 no element is left
#so last 5 remain its position
array.copyWithin(start_placing_index, start_from_index, end_index);
const array = [1,2,3,4,5];
array.copyWithin(2,3, 5);
# start copy from index 3 to 5-1 index and start place from index 2
console.log(array); // [1,2,4,5,5]
Apart from those so many changes are proposed like
Posted on December 26, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.