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

  • Home
  • SEARCH
  • 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 9022419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:30:22+00:00 2026-06-16T05:30:22+00:00

If I run the following code in google chrome console I get the following

  • 0

If I run the following code in google chrome console I get the following results

var x = 1;

alert(delete x); // false

eval('var y = 2');

alert(delete y); // true

Why in the first example the variable is not deleted and in the second example it is deleted?

  • 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-16T05:30:23+00:00Added an answer on June 16, 2026 at 5:30 am

    From the Mozilla JS Docs for delete:

    delete is only effective on an object’s properties. It has no effect
    on variable or function names.

    The example provided is similar to yours.

    x = 42;         // creates the property x on the global object
    var y = 43;     // declares a new variable, y
    
    delete x;       // returns true  (x is a property of the global object and can be deleted)
    delete y;       // returns false (delete doesn't affect variable names)
    

    So, why does alert(delete y); work? I couldn’t nail down the exact answer, but basically you cannot rely on the scope of eval.

    I think eval('var y = 2'); does not get declared as a variable and is treated as a property, but I haven’t found evidence for that yet other than the results of our tests. I’m going to continue researching to see if I find the exact reason.

    Other articles on eval weirdness:

    • http://wingolog.org/archives/2012/01/12/javascript-eval-considered-crazy
    • http://brownplt.github.com/2012/10/21/js-eval.html
    • http://blog.rakeshpai.me/2008/10/understanding-eval-scope-spoiler-its.html

    EDIT 0

    Based on @Xavier Holt’s comment, I looked up hoisting and scope in regards to eval. This scope cheatsheet from Mozilla Dev docs had the following:

    eval may capture assignments, but not var declarations

    eval’d vars hoist normally, so evals may capture assignments similar
    to with:

    function f() {   {
        let x = "inner";
        eval("var x = 'outer'");
        print(x); // "outer"   } 
    }
    

    If I’m reading this correctly, then my earlier assumption was right. eval() does not evaluate var declarations by declaring a variable. It must create a property or be treated as a property in order for delete to work.

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

Sidebar

Related Questions

when I trying to run following code. var result = from c in db.brand
When I run the following code I get B as expected: class A {
If I run the following code on my HTC magic phone I get a
When I run the following code, I get the exception below: ''# NOTE: ExcelApp
When I run the following code, it only seems to be downloading the first
I try to run the following code: public void Init(Url rootUrl) { var web
The following code works in Google Chrome and Firefox, but don't work in IE8.
I have the following code....and I can't seem to get the ads to run
I am attempting to run the following code snippet: var myVar1 = $.getJSON('myurl', function(json)
I run the following code from command line: public class MemoryTest { public static

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.