gridContainer.width = width * CELL_LENGTH; // 1
gridContainer.style.width = width * CELL_LENGTH; // 2
gridContainer.style.width = width * CELL_LENGTH + "px"; // 3
I just figured out that the code in cases 1 and 2 are wrong. The problem is js doesn’t complain about either of them. No errors in console. Nothing!
How should I know or figure out things like this?? When there’s no error and I don’t know why it doesn’t working other than trying different syntax until it works!
I used console and dev tools to figure it out as well but div.width seems to just adding another property to div that’s useless for browser.
However for the second case, It just refuses to assign wrong syntax value to div.style.width without any complaint


Js is a dynamic language. The first line is not “wrong”, it just isn’t what you want. It does indeed just add a new field in the gridContainer object. Tthis is perfectly valid javascript.
Js doesn’t “know” that it is running in a browser, nor does it care. It doesn’t know what the appropriate value or type is for these fields in a ‘div’ or anything else.
Yes, you pretty much have to use trial and error, unless there exists some other tool that can typecheck your code. I don’t do webdev, so I’m not sure.