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.
Related
This question already has an answer here:
Javascript Console Log reporting object properties incorrectly
(1 answer)
Closed 4 years ago.
I got a little problem.
m1 = new Matrix3D(null);
m1.initRotX(20);
console.log(m1);
m1.initRotY(20);
console.log(m1);
this is logging 2 times the same matrix.(the y rot Matrix)
when i initialize a new matrix every time it works fine.
Is js threading this and the init function is faster the the log or is there another explenation for this?
If you log an object to the console, you will see two things: one row (usually printed in italics), which shows the result of the object's toString() method at the moment when you called console.log(). The second row, which you can expand to view the detailed properties of the object, always shows the latest state of the object, no matter at what point the log was made.
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:
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
Closed 5 years ago.
Why chrome developer tool showing 0 array length (first line Array[0]) even if there are total 9 objects in it?
At first line of image should be like Array[9] why it is showing Array[0]
In second image array has 13 objects so it is showing Array[13] not Array[0]
It seems like you are logging the output of the array before you are adding objects to your array. Something like
var arr = [];
console.log(arr);
arr.push({}); //pushing blank obj
Here, executing this code will result in Array[0] but it does hold your object and will return a length of 1 if you try arr.length.
This might also happen if you are having some sort of Async function which pushes item to your array, which will result in the same thing. For example,
var a = [];
setTimeout(function(){
a.push({});
a.push({});
}, 1000);
console.log(a);
I think, this behavior is intentional, where Chrome wants to show that initially, your Array was empty, later on, items were pushed on runtime.
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/.
This question already has answers here:
Copy array by value
(39 answers)
Closed 8 years ago.
I'm creating a simple question and answer game to help with revision and having trouble repopulating an array the second time around.
When a button is clicked the following code is executed.
if(setSelection == 0){
tempQuestions = chosenQuestion;
tempAnswers = chosenAnswer;
}
This works perfectly the first time.
When a correct answer is selected the following code removes the question and answer from temporary array, leaving the original intact.
tempQuestions.splice(randomQuestion,1)
tempAnswers.splice(selectedAnswer, 1);
When the button is pressed for a second time, after the 'game' is complete, the temporary array fails to refill even though I'm executing the same code.
Any ideas why the code above does not work on the second run?
EDIT
jsfiddle
You are creating a new reference to the same array, so when you modify the temp vars you also modify the object referenced by the chosen vars. You need to copy the array. A nice way is to add your own copy() prototype method to the Array object.
a shallow copy should do:
Array.prototype.copy = function(){
return this.slice(0);
}
If you need a deep copy
Array.prototype.copy = function(){
return JSON.parse(JSON.stringify(this));
}
Use it like this:
if(setSelection == 0){
tempQuestions = chosenQuestion.copy();
tempAnswers = chosenAnswer.copy();
}
Using .slice works.
tempQuestions = chosenQuestion.slice();
The slice() operation clones the array and returns reference to the original.