this post was submitted on 24 Dec 2025
5 points (100.0% liked)

JavaScript

2621 readers
13 users here now

founded 2 years ago
MODERATORS
 
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

you are viewing a single comment's thread
view the rest of the comments
[–] IllNess 7 points 2 weeks ago

In Javascript width should not default to px considering you can also use em, px and %.

This won't help with your units issue but if you want Javascript to give more errors, try strict mode. Add: "use strict"; as the first line in your file.

MDN: Strict mode