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 6354283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:33:08+00:00 2026-05-24T22:33:08+00:00

I just spent a long time figuring out that I shouldn’t use clear() as

  • 0

I just spent a long time figuring out that I shouldn’t use clear() as the name of a function in Javascript:

<head>
    <script type="text/javascript" src="Array.js"></script>
</head>
<body>
    Hello!!!!<br>
    <button type="button" onClick="clear()" id="ppp">Shoo!</button><br>
    <button type="button" onClick="add()" id="add">Add a few elements</button><br>
    <button type="button" onClick="check()" id="check">Check the array</button><br>
    <p id="results">Results will appear here.</p>
    <script type="text/javascript">
initialize();
    </script>
</body>

Here’s Array.js:

var results;

function initialize(){
 results = document.getElementById("results");
}

function add() {
results.firstChild.data="add";    
}

function clear() {
results.firstChild.data = "Hello?";
}

function check() {
results.firstChild.data = "check";
}

Symptoms: Clicking the ‘add’ and ‘check’ buttons gives me the result I expect, but clicking the ‘clear’ button does nothing.

If I rename clear() to clearxyz(), it works fine.

My questions:

  1. Is “clear” a reserved word? I don’t see it on the list:
    https://developer.mozilla.org/en/JavaScript/Reference/Reserved_Words
  2. Is there a debugging trick I should be using to figure this kind of
    thing out in the future? It took me a long time (I’m a noob!) to
    figure out that the name of the function was my problem.

Many thanks.
Edit: I’m using Firefox 6.0, and I added a line break to show where Array.js starts.

  • 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-05-24T22:33:08+00:00Added an answer on May 24, 2026 at 10:33 pm

    As the others said, clear is not a reserved keyword. It seems that the called function is document.clear [MDN]. Invoking

    console.log(clear === document.clear);
    

    inside the event handler returns true.

    DEMO

    So it seems, document is in the scope chain of the event handler…. the question now is why.

    JavaScript: The Definitive Guide says:

    In an event handler as HTML attribute, the Document object is in the scope chain before the Window object (…)

    As your method is global, meaning it is a property of the window object, it is not found in the scope chain, as document.clear comes earlier in the scope chain.

    I haven’t found any specification for this. The guide also says that one should not rely on that, so I assume this is nothing official.

    If you have form elements inside a form, then even the corresponding form element will be in the scope chain (not sure whether this holds for all browsers though). This is another reason for confusion.


    There are two (not exclusive) ways to avoid such situations:

    • Don’t use inline event handlers. It is considered bad practice as it is mixing logic and presentation. There are other ways to attach event handlers.

    • Don’t pollute the global namespace. Create one object in global scope (with a name you are sure of does not collide with any window or document properties or ids of HTML elements) and assign the functions as properties of this object. Whenever you call a function, you reference it through this object. There also other ways to namespace your code.

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

Sidebar

Related Questions

I've just spent a long time trying to figure out why my Javascript for()
I just spent a very long time debugging an issue in python, using the
(Sorry if this is a dupe) I've just spent a long time trying to
I just spent a long time breaking my teeth on why this code was
I've spent a long time trying to work this out, but no success thus
I just spent way too long trying to diagnose why, in the following snippet
I just spent half an one our to find out what caused the Error-Message
I spent a long time learning how to customise WCF from the point of
I just spent a fair amount of time selecting the files and directories I
I've spent a long time working on layout issues and screen sizes. I've read

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.