This question already has answers here:
How can I unset a JavaScript variable?
(12 answers)
Closed 8 years ago.
For example if I type
var x = 5;
x;
console output is 5 (as expected)
But when I want to start afresh and clear the console(using ctrl+L or clear(); ),the console clears up but x still contains value 5 (as can be checked by typing x in console)
I don't want console to remember any previous data after using clear.How can I do it?
clear() does just that - clears the console.
But to add something new - you can't delete your variables, at least not when they're declared this way. The only 100% sure way of doing this, is simply refreshing the page.
To elaborate: you can delete only object's properties, so if you do this:
x = 5;
delete x; // returns true
It will work, but if you assign your value using var keyword:
var x = 5;
delete x; // returns false
...then you can't delete it.
To better understand what you can and what you can't delete and why - you can read more under this link: http://perfectionkills.com/understanding-delete/.
Related
This question already has answers here:
Why I'm Getting NaN? [closed]
(3 answers)
Closed 2 years ago.
I can't quite seem to figure out why the variables I created don't return the way I would like. Simple math problems here, multiplication and division. Creating the variable outside of the function returns NaN, but calling within the function returns what I suspect.
Snippets below:
var p = parseInt(document.getElementById('watts').value, 10);
var e = parseInt(document.getElementById('volts').value, 10);
function calcAmps(){
var p = parseInt(document.getElementById('watts').value, 10);
var e = parseInt(document.getElementById('volts').value, 10);
document.getElementById('answer').innerHTML = p/e;
};
Now, using the variable within the function work fine. The math executes without issue. But if I comment out the variables within the function and attempt to use the global variables, I get NaN.
Any thoughts? Should be a softball question.
When initialized outside the function, the values are only fetched from the DOM once, when the script first runs. Inside the function, the values are fetched at the point that the computation actually happens.
To be clear and explicit, the following:
var p = parseInt(document.getElementById('watts').value, 10);
does not mean that a permanent dynamic relationship is established between the variable p and the input field. Instead, it instructs the runtime to fetch the current value of the field at the time the code runs. After that point, nothing you type in the input field will be reflected as the value of p.
This question already has answers here:
console.log() shows the changed value of a variable before the value actually changes
(7 answers)
Closed 3 years ago.
Why does this show the same random number twice when you run it in your console?
var arr = [[]];
arr[0][0] = Math.random();
console.log(arr);
arr[0][0] = Math.random();
console.log(arr);
I derived this root issue from another post, which was not getting much attention, and I'm wondering if anyone has a clear explanation of what's going on here. I know there are things you can do to make this work as expected, but I'm interested in knowing why it doesn't work as expected right now. It's acting as if console.log() waits until everything else is through to log anything.
The console.log shows a reference to the array, the moment you expand it it retrieves the latest array value with the latest Math.random() value.
The following demo shows this:
var test = [1,2,3];
console.log(test)
test.push(4)
console.log(test)
The preview shows the old values but when expanding the values it retrieves it using the pointer and thus gets the new values.
This question already has answers here:
console.log() async or sync?
(3 answers)
console.log() shows the changed value of a variable before the value actually changes
(7 answers)
Closed 5 years ago.
Please share your thoughts regarding below outputs.
var a = '';
console.log(a); //this logs nothing as expected.
a = 'triven';
var a = {};
console.log(a); // How javascript is aware of property here that is defined below.
a.name = 'triven';
I think you're looking for console.dir().
console.log() doesn't do what you want because it prints a reference to the object, and by the time you pop it open, it's changed. console.dir prints a directory of the properties in the object at the time you call it.
The JSON idea below is a good one; you could even go on to parse the JSON string and get a browsable object like what .dir() would give you:
console.log(JSON.parse(JSON.stringify(obj)));
This question already has answers here:
Change the value of an array changes original array JavaScript
(3 answers)
Closed 6 years ago.
Maybe the question already exists, but I don't find anything because I don't know how to call this problem.
I have an object function and want to assign the object variable into a variable:
this.myFunction = function() {
var selection; // internal variable
selection = this.selection; // both are [0,0]
console.log(this.selection); // result: [0,0]
selection[0] = 500; // set internal variable[0] 500
console.log(selection); // result: [500,0]
console.log(this.selection); // result: [500,0] <-- expected: [0,0]
}
How I can change the local variable without changing the other?
When you call selection = this.selection, you are copying the reference of this.selection value. So when you change the local variable selection, you are changing this.selection too.
You have to use the method slice() to create a new array from the value. Like this:
selection = this.selection.slice();
This question already has an answer here:
Accessing a JSON property (String) using a variable
(1 answer)
Closed 8 years ago.
I want to use a string as a JSON property in JavaScript.
var knights = {
'phrases': 'Ni!'
};
var x = 'phrases';
console.log(knights.x); // Doesn't log "Ni!"
When I run this code, it obviously doesn't work because it interprets "x" and not the contents of the variable "x".
The full code in context on pastebin: http://pastebin.com/bMQJ9EDf
Is there an easy solution to this?
knights.x looks for a property named x. You want knights[x], which is equivalent to knights['phrases'] == knights.phrases.
Full code (fixing a couple of typos in your example):
var knights = {
"phrases": "Ni!"
};
var x = 'phrases';
console.log(knights[x]); // logs Ni!
Try this to access using variables having string values
kinghts[x]
Basically this is trick
kinghts[x]==knighted["phrases"]==knighted.phrases.
knights.x will get a key named x, So it'll return undefined here.
knights.x is the same as knights['x'] - retrieving a property under the key x. It's not accessing the variable x and substituting in the value. Instead, you want knights[x] which is the equivalent of knights['phrases']