Sitemap

What’s new in JavaScript 2019 (New feature)

3 min readJul 28, 2019

--

Press enter or click to view image in full size
New Javascript 2019

String.prototype.trimStart() or trimEnd()

we already had the trim() function. It removes blank spaces in a string. Now the new addition is the trimStart() and the trimEnd() functions, that help you choose which part of the string needs to be trimmed. This is a very cool addition for string manipulation operations

const mySample = “    Hey medium “;
console.log(mySample.trimStart());
// “Hey medium   “;
console.log(mySample.trimEnd());
// “    Hey There”;

Array.prototype.flat()

The new addition to the array prototype is the flat() function. This creates a new array and flattens the nested array. Let’s take a look at a sample on how it works.

const users = [[“Adam”, “1”], [“Jones”, “2”], [“John”, “3”]];
const flatUsers = users.flat();
console.log(flatUsers );// — — — — [“Adam”, “1”, “Jones”, “2”, “John”, “3”]

Object.fromEntries()

The fromEntries() method, will transform a list of key-value pairs into an object.

The Object.entries() function, that we already have converted an object, into key-value pairs.

Now the Object.fromEntries() does the exact reverse. Let’s take a look at an example with a user-id pair.

use of Object.fromEntries
const user = [
[“1”, “Adam”],
[“2”, “Jones”],
[“3”, “John”],
[“4”, “Smith”]
];
const userObject = Object.fromEntries(user);
console.log(userObject);// —{1: “Adam”, 2: “Jones”, 3: “John”, 4: “Smith”}

use of Object.entries
let objectKey={1: “Adam”, 2: “Jones”, 3: “John”, 4: “Smith”};
console.log(Object.entries(objectKey));
// — [
[“1”, “Adam”],
[“2”, “Jones”],
[“3”, “John”],
[“4”, “Smith”]
];

Optional Catch Binding

Before the error parameter in the catch was required.
try {
throw "Some error";
} catch (error) {
// Error handling logic
}

Now no need of

try {
throw “Some error”;
} catch{
// Error handling logic
}

Function.toString()

The toString() method returns a string representing the source code of the function.Earlier white spaces,new lines and comments used to remove now they are retained with original source code.

Fixes in JSON.stringify

JSON.stringify to prevent it from returning ill-formed Unicode strings. Previously, JSON.stringify would output lone surrogates if the input contained any lone surrogates:

``` JSON.stringify(“\uD800”);

Get Ranbir Kumar Das’s stories in your inbox

Join Medium for free to get updates from this writer.

// → ‘“�”’ ``` With this change, JSON.

stringify outputs escape sequences for lone surrogates, making its output valid Unicode (and representable in UTF-8):

``` JSON.stringify(“\uD800”); // → ‘“\ud800”’ ```

Let Vs Var Vs Const (This is not new but just added for good practice)

Both are the way to declare the variable. Var can be scoped in the entire method but let is accessible inside the loop scop.

  1. var — Can be re-assigned, re-defined and has a function-scope. When declared outside the function, it has a global scope and attaches itself to the window object.
  2. let — Can be re-assigned. Its scope is within a block of code.

let’s check the example
var string_var=” This can be accessed through window object”
go to the console and check the window object

window.string_var, this will show the value.

This is the major impact if your third party library using the same variable with the var keyword. In other sense, let is not going to store in the window object.

Let’ check with the function scope
Use of Let

function hello (){
for (let i=0;i<5;i++){
console.log(i); // — — — This will show the i value

}

console.log(i);// — — — undefined
}

Use of Var

function hello (){
for (let i=0;i<5;i++){
console.log(i); // — — — This will show the i value

}

console.log(i);// — — — This will retur the value
}
conaole.log(i);// — — — — — undefined

Use of const

Use of Const

const is another addition in the ES6 JavaScript standards. It is similar to let, where it also adheres to the block-scope. The difference between const and let can be understood with a simple example below.

const x=”hello”;
let y=”medium”;
y=”Hi” // — — — — — — you can reassign the value
x=”javascript”;// — — — — will throw an error when x is :TypeError: Assignment to constant variable.

Find out all the new things which are in the proposal stage here.

--

--

Ranbir Kumar Das
Ranbir Kumar Das

I M Believer, Helper, Chaser, Thinker, Rich, Explorer, Prayer, Boss, Freedom, Fearless, Investor, Faith, Creator, trillionaire, CSM, Salesforce certified