Home > Enterprise >  Uncaught ReferenceError: function is not defined error for 1 function
Uncaught ReferenceError: function is not defined error for 1 function

Time:09-25

I think it's a very common problem but I've a code like this:

function add(num1, num2) {
  return num1   num2;
}

function hello() {
  return "Hello";
}
window.alert("Num1   Num2 = "   add(10, 5));

window.alert(hello());

The thing is, add() function works perfectly but try() is not. Why? I am working on Visual Studio Web Application.

CodePudding user response:

Its becuase try is a reserved keyword in javascript as in try-catch, similarly you cannot use keywords like let, const, var, do, for ... etc as variable names or function declarations, change your function name to hello or something else and it should work perfectly fine

CodePudding user response:

That's because try is a reserved word, have you ever seen

try {

}catch(e){}

So change the name of the function, simply use try1()

  • Related