1
var functionVariable = function functionExpressionName() {
    functionExpressionName = 1;
    console.log(functionExpressionName) // function
};

functionVariable();

If You run this example you can see we can not reassign to functionExpressionName anything. But this is also interesting we can reddeclare functionExpressionName and after this we can assign anything to functionExpressionName

var functionVariable = function functionExpressionName() {
    function functionExpressionName() {

    }

    functionExpressionName = 1;
    console.log(functionExpressionName); // 1
};

functionVariable();
6
  • Yeah that's look like but in there not say Function Expression and when I search I don't found in stackoverflow. Sorry about that I close question :) Commented Aug 17, 2019 at 9:23
  • Why U delete first comment? :d Commented Aug 17, 2019 at 10:05
  • Which comment do you mean? Commented Aug 17, 2019 at 10:06
  • Link to main question Commented Aug 17, 2019 at 10:07
  • 1
    The “possible duplicate” comments get deleted automatically once the question is closed with that duplicate target. The link is at the top of your question and in the “Linked” sidebar. Commented Aug 17, 2019 at 10:08

1 Answer 1

3

If you enable strict mode, the error becomes a bit clearer:

'use strict';
var functionVariable = function functionExpressionName() {
    functionExpressionName = 1;
    console.log(functionExpressionName) // function
};

functionVariable();

Uncaught TypeError: Assignment to constant variable

The function name is un-reassignable inside the function, but you can create a new variable with the same name inside the function body. One way of looking at it is that the function name is declared with const just outside the function body:

var functionVariable = (() => {
  const functionExpressionName = function () {
    functionExpressionName = 1; // Clearly wrong - functionExpressionName is a const
    // but it would work if you declared a *new* variable,
    // which has a different lexical binding
    console.log(functionExpressionName) // function
  };
  return functionExpressionName; 
})();

functionVariable();

This isn't exactly what happens, but it's pretty close.

Sign up to request clarification or add additional context in comments.

1 Comment

Make Sense Thank You Very Much :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.