Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript, let function if not defined

Tags:

javascript

Every time a modal is opened I load via ajax some html + js, like this:

var loadModalJs = function() {
   ...
}

$(function() { loadModalJs(); });

It works with var, but if I replace var with let, when the modal is closed and then opened again I get Identifier 'loadTabJs' has already been declared

I tried something like this, without success:

if(typeof loadModalJs === 'undefined') {
    let loadModalJs = function() {
       ...
    };  
}
loadModalJs(); // ReferenceError: loadModalJs is not defined

I don't like using var because my IDE complains about "'var' used instead of 'let' or 'const'"... But none of them seems to work... How could I do?

like image 297
Sofia Grillo Avatar asked Mar 09 '21 07:03

Sofia Grillo


People also ask

How do you check function is defined or not in JavaScript?

Use the typeof operator to check if a function is defined, e.g. typeof myFunction === 'function' . The typeof operator returns a string that indicates the type of a value. If the function is not defined, the typeof operator returns "undefined" and doesn't throw an error.

How do you check if a variable is not defined in JavaScript?

Summary. In JavaScript, a variable can be either defined or not defined, as well as initialized or uninitialized. typeof myVar === 'undefined' evaluates to true if myVar is not defined, but also defined and uninitialized. That's a quick way to determine if a variable is defined.

Is Let better than VAR?

let can be updated but not re-declared. This is because both instances are treated as different variables since they have different scopes. This fact makes let a better choice than var . When using let , you don't have to bother if you have used a name for a variable before as a variable exists only within its scope.

Is var replaced by let?

let and const are block-scope variable declarations that can replace 'var' declarations in many cases.

How to check if a variable is not defined in JavaScript?

Whenever you create a variable and do not assign any value to it, by default it is always undefined. It becomes defined only when you assign some value to it. There are numerous ways to check if a variable is not defined. We are going to use one of the easiest solutions which involve the usage of the typeof and ternary (?) operators.

What causes the function is not defined error in JavaScript?

Finally, the function is not defined error can also be caused by calling the function before the script is loaded to the browser. Suppose you have a JavaScript file separated from your HTML file as follows: Then you load the script into your HTML file, but you call the fnAction function before you load the script as follows:

Can a function be defined through a function expression?

Please keep in mind that functions defined through function expressions must be defined before the call. Function expressions are functions that you defined through a variable keyword as follows: From the example code above, the variable fnAction will be hoisted, but the function declaration is not, so it will be undefined as shown below:

What are functional expressions in JavaScript?

Function expressions are functions that you defined through a variable keyword as follows: From the example code above, the variable fnAction will be hoisted, but the function declaration is not, so it will be undefined as shown below: That’s why it’s always better to define the function before calling it.


Video Answer


2 Answers

The error you placed into the comment is correct: at no point do you define the variable in scope -- you define it within the the if clause, and it is not available outside of that clause, your reference to it in the if statement is to an undefined global variable.

Also, if your variable is only ever going to be undefined or a function, then no need to test its type, as undefined evaluates to false.

let loadModalJs;

// ...your other code here...


// This bit within the function/scope run multiple times:
if (!loadModalJs) {
    loadModalJs = function() {
       ...
    };  
}

if (loadModalJs) { 
  loadModalJs(); 
}
like image 188
Lee Goddard Avatar answered Oct 17 '22 04:10

Lee Goddard


In addition to the current answer:

Your second snippet could work if you use var instead of let.

As if creates its own block {}:

  • let is limited to the immediate block {}.
  • var is limited to the immediate function block {} (or global if there are no functions).

E.g.:

if (true) {
  var x = "It'll work outside the if.";
}

console.log(x);

Edit:

As an additional tip, you might be careful with the way you 'create' functions in javascript:

  • Functions defined, e.g. let myFunction = function() { }, are under top-to-bottom flow of control (i.e. first define, then use).
  • Functions declared, e.g. function myFunction() { }, are hoisted (i.e. declare and use where you want, in the same scope).
like image 20
Leo Avatar answered Oct 17 '22 04:10

Leo