Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8546457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:01:38+00:00 2026-06-11T13:01:38+00:00

Is it possible to detect duplicate functions in Javascript (which may be written by

  • 0

Is it possible to detect duplicate functions in Javascript (which may be written by accident in some cases)? In Google Chrome,

printLah(); //this prints "Haha" for some reason, without even printing an error message in the Javascript console!
function printLah(){
    alert("Hahah!");
}


function printLah(){
    alert("Haha");
}

Here it is on JSfiddle.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:01:39+00:00Added an answer on June 11, 2026 at 1:01 pm

    The short answer is, no it’s not.

    This is how javascript works. A function name is just a variable that is assigned a function. For example:

    function foo () {
        alert('foo!');
    }
    
    foo = 1;
    
    foo();
    

    The code above will generate an error because a number is not a function! There is no difference between function names and variable names. In fact, an alternative way to define functions looks exactly like how you’d define variables:

    var foo = function () {
                  alert('foo!');
              }
    

    It’s because of this functions-as-first-class-objects behavior that javascript cannot prevent re-assignment otherwise you cannot re-assign variables (pure functional languages on the other hand have no problems with disallowing variable re-assignments).


    Work-arounds and best practice:

    This is the reason people keep saying that you shouldn’t define too many globals in javascript. That includes functions. Otherwise it may clash by accident with someone else’s code.

    There are two powerful features in javascript that can mitigate this problem: objects and closures.

    Because javascript supports objects you should use object oriented programming to limit the number of globals in your program. Unlike traditional OOP, javascript works better when using objects as collections or namespaces. This is because javascript doesn’t have file scope and everything is global.

    This doesn’t mean you shouldn’t create smaller objects that encapsulate smaller problems like you do with traditional OOP. It just means that you should, if you can, contain all your objects within a single parent object. And I don’t mean inheritance here, I mean in a has-a relationship. Look at popular libraries like jQuery and Raphael for examples of this. They export only one object to the global scope to avoid clashing with other people’s code.

    But, again, this doesn’t really protect people from re-assigning your objects (because they’re variables after all). For example, you can easily break jQuery by doing this at the top of your HTML file before other bits of javascript have the chance to run:

    jQuery = null;
    

    The way to protect your code from being tampered with is to use a closure. Third party code has no access to any code you run from inside your closure. As long as you avoid globals that is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to detect if JavaScript is disabled? Is it possible to detect
Possible Duplicate: How to detect right mouse click + paste using JavaScript? How to
Possible Duplicate: How to detect if JavaScript is disabled? I'm curious what other people
Possible Duplicate: Which keycode for escape key with jQuery This little piece of code
Possible Duplicate: How to detect true Windows version I have an application which uses
Possible Duplicate: How can I detect the browser with PHP or JavaScript? I need
Possible Duplicate: javascript detect browser close tab/close browser I have a java script countdown
Possible Duplicate: Android - detect whether there is an Internet connection available this is
Possible Duplicate: How to detect if JavaScript is disabled? My first web application relies
Possible Duplicate: How to detect an android device whether it supports google maps API

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.