Q:

How does calling JavaScript functions work?

I'm a student and I'm coming from a lower level language (Java), so I'm very confused about this.
For example:
function sum (x){
let z = function (y) {return x + y };
return z;
}
sum(1)(2) // returns 3

How does sum(1)(2) work? I thinksum(1) returns z, upon which z(2) is called but I don't quite understand this. Also, why can you just initializez as function (y) without giving that function a name? Any tips on approaching JavaScript from a lower-level language perspective?
I'm having a hard time.

A:

 How does sum(1)(2) work?
You are calling sum(1) and than you call the return value with (2). In this case, the return value is a function, so you can call it. It's the same as writing it like this: 

function sum(x) {
  let z = function(y) {
    return x + y
  };
  return z;
}
let returnValue = sum(1);
console.log(returnValue);
console.log(returnValue(2));



As you can see, returnValue is a function, like expected. This function takes 1 argument, y, and use the x from the first function's creation scope to return x + y.
Why can you define z as function without a name?
When you are using the syntax let z = something you are using an expression to define the something. Just like you will define let a = 1 and not let a = myVar 1 (??), the same with functions. You don't need to give it a name because you can refer to the function as z. Got it?
Any tips on approaching JavaScript
If you wanna learn JS, I really recommend you to read You Don't Know Javascript. Iv'e read it after I thought I know JS, and Iv'e discovered a lot of things that I didn't knew about.
It also explains how things works in js. 
More over there are a lot of websites that can teach and train you. Just google it.
IMPORTANT NOTE
java is not a low level language but a high level one.

