DEV Community

Lanalen
Lanalen

Posted on

What I learnt today: Function

Declaration vs Expression

Declaration

function myFunction() {
    console.log("hi");
}
Enter fullscreen mode Exit fullscreen mode

Expression

const a = function() {console.log("hi")};
Enter fullscreen mode Exit fullscreen mode

The Function Constructor : Function()

usage

const a = new Function("x", "y", "return x + y");
Enter fullscreen mode Exit fullscreen mode

also possible without new keyword

const a = Fuction("x", "y", "return x + y");
Enter fullscreen mode Exit fullscreen mode

Function Hoisting

What is it? To call a function before declaring it.

myFunction();

function myFunction() {
    console.log("hi");
}
Enter fullscreen mode Exit fullscreen mode

Parameter vs Argument

function myFunction(name) {
     console.log("Hi", name);
}

myFunction("Lena");
Enter fullscreen mode Exit fullscreen mode

name is a parameter.
"Lena" is an argument.


Self-Invoking Function

What is meant by "self-invoking"? You don't need to call the function.
How to use? Put a function inside parentheses.

(function () {
     console.log("hi");
})();
Enter fullscreen mode Exit fullscreen mode

Latest comments (0)