I have a script with the function addBorder()
which sets the first div's a border style property to "2px solid red"
. But when I console.log()
the div
, the border is already added before the function call for addBorder()
.
let div = document.querySelector("div");
function log(){
console.log("before addBorder() called: ", div);
}
function addBorder(){
console.log("addBorder() called");
console.log("before css property set: ", div);
div.style.border = "2px solid red";
console.log("after: ", div);
}
log();
addBorder();
<div>one</div>
Here's what the console shows in Chrome Devtools:
As you can see, the border is already added before the function call for
addBorder()
and before the .style
property is set. What I would expect is for the first two logs to be <div>one</div>
, and the last to be <div style="border: 2px solid red;">one</div>
, since that is the only log called after the property is set.
Maybe this is a bug in Chrome devtools, because it actually works as expected in the stackoverflow console for the code snippet on this post.
Another strange behavior- if I slowly step through the code in the debugger the logs look completely different:
But the result here isn't what I'd expect either, since the last log shows the original state of with the div, <div>one</div>
without the border style applied- style="border: 2px solid red;"
.
Is this strange behavior due to some flaw in my code? Or is it a Chrome devtools bug?