javascriptreturnreturn-value

What does a function return if it does not contain a `return` statement?


I've watched some videos and they said "functions always return a value". If we don't use the return statement, then what does it return instead? Like the function below:

function Foo(){
    var x = 4.
    y =5;
    z = x + y;
}
x = Foo();
console.log(x);

Solution

  • The function should return an 'undefined' property when it is called with x

    There is generally two scenario:

    // 1
    console.log(x)
    
    // 2
    console.log(x())
    

    In scenario 1 (which is depicted in your example), it will return a reference of the function (which can be used to execute later). In scenario 2, it will executes the function and return an undefined property. Do check out this website if to know more

    undefined is a property of the global object, i.e. it is a variable in global scope. The initial value of undefined is the primitive value undefined.