0

I was reading some training code and I could see that example 1 doesnt use function in the it statement, but in Example#2 I can see it, notice that I am totally new in this world of JS and Protractor, so any feedback will be really appreciate it.

I have search for information but not sure of the difference

Example#1

it('should have a history', () => {
    add(1, 2);
    add(3, 4);
    expect(history.count()).toEqual(2);
    add(5, 6);
    expect(history.count()).toEqual(3);
}); 

Example#2

it('should have a history with text', function () {
    add(1, 2);
    add(3, 4);
    expect(history.last().getText()).toContain('1 + 2');
    expect(history.first().getText()).toContain('3 + 4'); 
});
Paul
  • 1
  • 1
  • 1
    tl;dr: they are different, and the difference isn't specific to protractor –  Aug 19 '19 at 21:49

2 Answers2

2

The first example presents an arrow function (also called lambda), it's a shortcut for making a function. The second example describes a full function declaration. There is no difference between the two, you can use whatever way you like.

Arrow function can be expressed like this:

var anon = (a, b) => a + b;

While a regular function looks like this:

var anon = function (a, b) { return a + b };

Both functions perform the exact same action.

0

Its the same thing. () => {} is an ES6 shorthand for writing function() {}. Just cleans up the code a bit and stops you from having to write function a thousand times.

Jbluehdorn
  • 475
  • 2
  • 11