-1

I've been having trouble with function expression and anonymous function.

let sayHi = function() {
  alert( "Hello" );
}; //This is function expression

var anon = function() {
  alert('I am anonymous');
}
anon(); //This is anonymous function

I don't see the difference between those two. From i see, they look the same to me. Can anyone please give me a better explanation of it so i can understand it. I appreciate it. Thank you

Mary
  • 13
  • 1
  • 3
  • 4
    The second example you posted is not an anonymous function. – Dai Mar 17 '20 at 01:12
  • there is no difference between the two, because they are the same thing with comments and names that make it appear they are different, when in fact, they are not – Jaromanda X Mar 17 '20 at 01:13
  • 1
    Also `sayHi` (the variable) is not a function-expression either. The `function` it was assigned from *is* a function-expression, however. – Dai Mar 17 '20 at 01:14
  • 1
    Your usage of terminology, as others point out, is incorrect. In programming trying to *infer* the meaning of a technical term from common usage is fraught with peril. "Expression", "anonymous", and "function" all have very specific technical meanings. An expression is something that can be assigned to a variable. Contrast with a statement like if/else that cannot. So a function expression is just a function that is assigned to a variable, meaning sayHi and anon are both function expressions. An anonymous function is one without a name. But in current Javascript assigning a function to a – Jared Smith Mar 17 '20 at 01:50
  • 1
    variable more-or-less gives it the name of the variable it's bound to. But traditionally you would call both of them anonymous function expressions. Not that much of any of this matters for actually writing day-to-day JS. – Jared Smith Mar 17 '20 at 01:52

4 Answers4

1

I think the better way of understanding is

let sayHi = function someName() {
  alert( "Hello" );
}; //This is function expression but with name and function can be called either way like sayHi() or someName().

var anon = function() {
  alert('I am anonymous');
}
anon(); //This is also function expression but without name to the function hence it is anonymous function.

An anonymous function is a function without a function name. Only function expressions can be anonymous, function declarations must have a name.

function() {
  alert('Hi')
}); // When used as a function expression 
(() => {
  alert('Hi')
}); // or using the ECMAScript 2015 arrow notation

MDN link to mozzilla developer network for more on functions

pavan kumar
  • 823
  • 6
  • 15
-1

A function expression is an expression which defines a function. Function expressions can be used to define a named or unnamed (anonymous) function. If you define a named function in an expression the name will only be local to the function itself. This can be useful for a recursive function.

let x = function foo(a){
    return 2 * a;
};

x(3); // 6
foo(3); // error! foo is undefined in this scope

let y = function recursive(a){
    return a <= 0 ? 0 : a + recursive(a-1);
};

y(4); // 10
recursive(4); // error! recursive is undefined in this scope

Function expressions can only be used after they are declared. So this gives an error:

x(); // error! x is undefined

let x = foo(){
    return 2;
};

This is in contrast to function declarations (different than mere definitions) which are hoisted to the top and can be called anywhere within the scope they would be defined in.

-1

The main difference is that a function declaration is 'declared'. In other words, it has been defined with a name and can be later referenced with said name.

function imDeclared() {
   return 'hello';
};

imDeclared(); // 'hello'

On the other hand, an anonymous function does not have a name assigned to it. (used as callbacks frequently)

spookyNoName( function () {
   // hello from within an anonymous function!
});

The parameter passed into spookyNoName is a function, but it is not named.

-1

Function Expression: An valid javascript expression in which function ( named / anonymous ) is used. Anonymous function: in simple words, its a function without a name.

let a = function(){return 1; }() + function(){return 2;}();

The above line is called function expression because the function is used in an expression. If we consider function alone, its called anonymous function.

function(){return 1;} 
ravindar
  • 406
  • 4
  • 13