How can function() work if it was never defined

Compare Scopes of the var and let Keywords course from ES6

An example shows the difference between let and var:
image

how does the function() work
Im not sure how the function() works
Im not sure how it assigns a value to the variable printNumTwo
why dont we exit the statement at the return i line?
Im not sure i understand the purpose of the return i line,

! i didnt even know we could name a function, function()

That function is defined on the same line that’s assigning it printNumTwo name. The name of the function itself isn’t function(), but rather it doesn’t have name when it’s defined. Sometimes such functions are called annonymous functions, because they don’t have their own name.

It’s in a function, return is how you get a value out of a function.

function example1() {
  return "example";
}

let example2 = function () {
  return "example";
}

:point_up: example1 and example2 are examples of defining functions in JavaScript

You can’t name anything function, it’s a keyword that denotes a function.

Like

let myArray = [1, 2, 3];
let myBool = true;
let myFunction = function () { 
  return "this is my function";
};

I haven’t called anything [1, 2, 3], or true, or function. I’ve assigned an array to a variable called myArray, a boolean to a variable called myBool, a function to a variable called myFunction.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.