Reputation: 57
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.
tools.forEach(tools => console.log(tools));
tools.sort().forEach(tools => console.log(tools));
Upvotes: 0
Views: 95
Reputation: 116
You can use this babel compiler to convert code examples from ES6 to ES5
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
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
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
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