The difference between | 0 and Math.floor

They don't do the same thing.

by minitech

CSS

table {
    border-collapse: collapse;
    font-family: monospace;
}

tr > * {
    border: 1px solid #ccc;
    padding: 0.5em;
    min-width: 7em;
    text-align: center;
}

JavaScript

function test(n) {
    document.write('<tr>');
    document.write('<td>' + n             + '</td>');
    document.write('<td>' + Math.floor(n) + '</td>');
    document.write('<td>' + (n | 0)       + '</td>');
    document.write('</tr>');
}

document.write('<table><tr><th>n</th><th>Math.floor(n)</th><th>n | 0</th></tr>');
test(64.6);
test(-5.5);
test(0x80000000); // -2147483648 as a signed, 32-bit integer.
document.write('</table>');