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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:13:16+00:00 2026-05-27T02:13:16+00:00

Not sure if this is considered best practice or if you should even do

  • 0

Not sure if this is considered best practice or if you should even do this but I have a small block of Javascript and I want to know if you can declare a variable, display that variable and then reassign it and display it again? Syntactically this seems correct but I would assume that this is not best practice and should be avoided?

Note: I did not write this block I just want to know if it’s ok or if I should change it and use 2 variables code below:

var u1 = 'something';

if (u1.indexOf('Accept') > 0)
{       
var URL = 'some URL';
document.writeln(URL);
URL = 'another URL';
document.writeln(URL);
}

Thanks in advance.

EDIT:Thanks for the answers, thought it was a bit daft. :/

  • 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-27T02:13:16+00:00Added an answer on May 27, 2026 at 2:13 am

    Yes you can

    You can change variable’s value as many times as you need to. Variables are quite often reused so we save memory resources. Not in the way you’ve used them (because that’s an example that would be better off providing constant strings directly when calling functions) but think of an everyday example where we don’t even think of multiple variable value assignments. A for loop:

    for (var i = 0; i < 100; i++)
    {
        ...
    }
    

    In this loop variable i gets assigned a new value 101 times. This is a rather obvious example, where we don’t think of this at all, but other than that, we could have a set of loops and reuse the same variable more explicitly and assign it a value lots of times like:

    var counter = 0;
    for(var item = GetLinkedListFirstItem(); item != null; item = item.Next)
    {
        counter++;
    }
    // other code...
    counter = 0;
    while (counter < 10 || someOtherCondition)
    {
        // do something else
    }
    

    This may be a much better example of explicit variable reusability where its value gets changed lots of times and for different purposes.

    Variable naming

    Variable reuse is sometimes unwanted/undesired. And that’s when we have a meaningful variable name like isUserLoggedIn. It’s hard to reuse such variable for other purposes because it would make code unmaintainable.

    Variables that are usually reused may hence be iterators (ie. i) or generally named variables without too much meaning. Or variables with more universal name (ie. finished) which can be reused in different contexts that can be associated with such variable name.

    Asynchronous code

    There are certain situations where you may have problems even though looking at code may seem perfectly fine. And that’s when you use async functions which is frequently the case when using Ajax calls or time-deferred calls (ie. setTimeout). Consider the following code:

    var loaded = false;
    
    $.ajax({
        url: "...",
        type: "POST",
        success: function(){
            loaded = true;
        }
    });
    
    if (loaded === true)
    {
        // do something important
    }
    
    // ok loaded not used any more, so we can reuse it
    // we can easily change its type from number to string or anything else
    loaded = "Peter loaded his gun";
    

    This code has a bug, because important code won’t be executed. Ever! This is quite a frequent misconception by unsavvy developers not understanding asynchronism.

    Hint: When code issues an Ajax call it doesn’t wait for a response but rather continues execution and executes if statement. Even though Ajax call would respond in 0time ticks, success function wouldn’t execute until this currently running code wouldn’t finish execution. That’s how Javascript works. Queued code execution. In the end when Ajax async code would execute it would eventually overwrite the string that was stored in the variable.

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

Sidebar

Related Questions

I’m not sure this is the best example, but here goes. Let’s say I
Not sure this is a programming question, but we use LaTeX for all our
Not sure this is possible, but looking to write a script that would return
I'm not sure this is possible, but in ruby, you can dynamically call a
I'm not sure this is possible, but is there a syntax to be used
I'm not sure this question is appropriate here but I hope I could get
I'm sure this is a fairly trivial problem, but I'm not sure what to
First of all I'm not sure this is even possible, however I need to
I'm not sure how best to describe this, or the best title, so bear
Consider this example: zh_Hant_HK format = yy'年'M'月'd'日' ah:mm Not sure if you can see

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.