DEV Community

Discussion on: 5 JavaScript "tips" that might bite you back.

Collapse
 
chrdek profile image
chrdek

Syntax bite # 5 for '12px 15px' can be also rewritten as:
margin.split(/px/g).map(Number) or margin.split('px').map(Number).
Produces a result of [12,15,0]. Still usable in case you only need to do basic calculations (add, subtract).

Collapse
 
paulsmithkc profile image
Paul Smith

You're still assuming all of the values have a unit and that unit is pixels.

This doesn't work for zero.

Collapse
 
chrdek profile image
chrdek

Great, thanks for pointing that out. Aside from that, it is an alternative for the specific example using integer casting.

Thread Thread
 
paulsmithkc profile image
Paul Smith

You missed the whole point of this article. Which that you should not be writing code that way in the first place. Nor should you be suggesting this buggy code to anybody else.