binarie
binarie

Reputation: 57

ES 6 to ES 5 need help fixing

I have two pieces of code that are ES6, but I need to use ES5 for a class. What is the best way to create each of these listed in ES5? I am outputting these from an array and each of the values in the array needs to be on a new line.

  1. tools.forEach(tools => console.log(tools));

  2. tools.sort().forEach(tools => console.log(tools));

Upvotes: 0

Views: 95

Answers (4)

Conor Heena
Conor Heena

Reputation: 116

You can use this babel compiler to convert code examples from ES6 to ES5

https://babeljs.io/repl#?babili=false&browsers=&build=&builtIns=false&spec=false&loose=false&code_lz=C4exBsGcDpJAnYAKAlNAZggogQwMYAWSoEkABALwB8ZeIAdnOAKbTggDmxYUKKA3EA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=es2015%2Creact%2Cstage-2&prettier=false&targets=&version=7.3.4

tools.forEach(tools => console.log(tools));

becomes:

tools.forEach(function (tools) {
  return console.log(tools);
});

and

tools.sort().forEach(tools => console.log(tools));

becomes:

tools.sort().forEach(function (tools) {
  return console.log(tools);
});

Upvotes: 1

A.A Noman
A.A Noman

Reputation: 5270

Replace => by using function

1st

tools.forEach(tools => console.log(tools));

Replace by given code

tools.forEach(function(tools){
    console.log(tools));
});

2nd

tools.sort().forEach(tools => console.log(tools));

Replace by given code

tools.sort().forEach(function(tools){
    console.log(tools));
});

This is the link to convert ES6 to ES5

Upvotes: 0

Jack Bashford
Jack Bashford

Reputation: 44087

Just transpile it using function like so:

tools.forEach(function(tool) {
    console.log(tool);
});

And add the sort for the other one:

tools.sort().forEach(function(tool) {
    console.log(tool);
});

Note that although there is an implicit return in the ES6 you provided, you don't actually need it in a forEach() loop, which is why I have excluded it - feel free to add it back in if you'd like.

Upvotes: 2

Mark
Mark

Reputation: 2071

The only difference is with the => you can simply write it like so;

tools.forEach(function (tools) {
  return console.log(tools);
});

Upvotes: 1

Related Questions