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.
Related
This question already has answers here:
How can I clone a JavaScript object except for one key?
(25 answers)
Closed 6 months ago.
I have a JS object that i want to log to the console but one of its values is 5 million characters of a hashed image so im not really interessted in that. Is there a way to exclude this key from the console log / is there a short form for cloning the object, deleting the value and logging it?
Not a duplicate of "cloning object except for one key" btw
One very easy way to do this is by using the spread operator [MDN Docs]
const yourObject = {id:"something",img: "5M-character-stuff"}
console.log({...yourObject, img: "short-img"});
// logs: {id:"something",img: "short-img"}
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:
How can I make console.log show the current state of an object?
(12 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 3 years ago.
I setup a global variable to hold the data array:
var data = [];
This is a global variable as it is used outside of a function and needs to be accessible for all functions
I then iterate over a data set and populate the data array.
When I access this array using the console:
console.log(window.data);
I can see the following in Chromes console view:
There are many more records in the data set, they all follow exactly the same format
The issue I am having is, I cannot access this data array and am seeing zero length as shown below:.
console.log(window.data.length); // returns 0
I'm not clear on why this is occurring and am hoping someone can point out what I am doing wrong.
It seems like the result of processing this array is not available at the time I am trying to get the length, therefore I am getting zero.
Have tried the following at the end of the html file:
$( document ).ready(function() {
console.log("ready!"); // returns ready!
console.log(window.data[15]); // returns undefined
});
Not quite sure how to proceed, so any advice would be very welcome.
EDIT:
This was resolved by the use of promises (bottom link in the duplicate notice above).
Previous code was removed as it didn't add to the discussion and would probably only create confusion
Core issue was based on data not being available on page load due to processing of large arrays.
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 an answer here:
how can I override console.log() and prepend a word at the beginning of the output?
(1 answer)
Closed 7 years ago.
Here is the example
var x = "foo";
console.log(x);
=> x: foo
is it possible to override console.log() / warp the console.log() / npm package / _ etc... can help ?
It's not possible to do what you've shown in the question, no, for the simple reason that the value of x is passed to the function. There is no connection from the argument that the function receives back to the x variable or the context in which the x variable exists, and so there's no way for console.log (or any replacement of it you would author) to determine that the name was, in fact, x.
Since console.log accepts multiple arguments, you can do:
console.log('x', x);
...which is a simple and easy way to get output similar to what you want.
You can also log a temporary object:
console.log({x:x});
In ES2015 (aka ES6) that can even just be:
console.log({x});