
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Can re-declaring a variable destroy the value of that variable in JavaScript?
Re-declaring a variable will not destroy the value of a variable, until and unless it is assigned with some other new value.
If we look at the following example variables "x" and ''y'' were assigned with values 4 and 8 respectively, later on when those variables were reassigned, the old values were replaced with the new values and displayed as shown in the output.
Example
<html> <body> <script> var x = new Number(4); var x = 7; var y = 8; var y = 10; document.write(x); document.write("</br>"); document.write(y); </script> </body> </html>
Output
7 10
In the following example, the variables were re-declared, but their values were not reassigned. Therefore those variables retained their original values.
Example
<html> <body> <script> var x = new Number(4); var x; var y = 8; var y; document.write(x); document.write("</br>"); document.write(y); </script> </body> </html>
Output
4 8
- Related Articles
- What is the difference between Declaring and Initializing a variable in JavaScript?
- What happens if we re-declare a variable in JavaScript?
- Must you define a data type when declaring a variable in JavaScript?
- Difference between declaring a variable before or in a Java loop.
- Extract the value of the to a variable using JavaScript?
- How can we store a value in user-defined variable?
- Update MongoDB variable value with variable itself?
- Setting a default variable value to undefined in a function - JavaScript?
- How to display JavaScript variable value in alert box?
- How can we assign a function to a variable in JavaScript?
- Getting an HTML H1 value to JavaScript variable?
- Variable Hoisting in JavaScript
- How to change the value of a global variable inside of a function using JavaScript?
- How can I check whether a variable is defined in JavaScript?
- How can we write MySQL handler, in a stored procedure, that sets the particular value of a variable and continues the execution?

Advertisements