JS Alert() Method Not Working - Answered

I am trying to run an Alert(), but it will not work. Here is the code:

HTML:

Press Me

JS:

function alert() {
alert(“Hey”);
}

Hey,

  1. alert() is an in-built method and by defining another function with the same name, you rewrite the initial one.
  2. You are calling alert within alert which causes the code infinitely call alert - recursive calls.

Any ideas now how to fix your code to call the original alert from inside another function?

// 1. Example
function sayHi() {
  console.log("hi");
}

function sayHi() {
  console.log("hello");
}

sayHi(); // results in "hello"
//  2. Example
function sayHi() {
  console.log("hi");
  sayHi();
}

// results in infinite calls and 
// the code will throw max recursion error
sayHi();
// "hi"
// "hi"
// "hi"
// ...

Thank You So Much, Big Help, Thank You!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.