What does a javascript object dataset attribute combined with a bitwise or and 0 do?

54 views Asked by At

I came across this pen when searching for a way to make a textarea auto-expand with input: https://codepen.io/vsync/pen/frudD. The code includes the line

var minRows = elm.getAttribute('data-min-rows')|0, rows;

I do not understand what the bitwise or and 0 after getting the dataset attribute does. I tried removing them, but that breaks the code.

Also, I think that the ", rows" is a remnant from a previous version of the code as "rows" is not defined until later in the code, and I can remove that and the code still appears to work (please correct if I am missing something).

1

There are 1 answers

0
i'm a girl On

The bitwise OR and 0 serves to convert the string property into a number to work properly. This can be verified by checking the type of the variable.

Usually, the stuff after the comma is used as the result, but since it is undefined, whatever comes in front is used instead (I think).