ก่อนมี Bitwise operator ต้องแปลงตัวเลขเป็นสตริงก่อน แล้วใช้ string method ตัด ตอนนี้ Bitwise OR (|) ทำให้ง่ายขึ้น ไม่ต้องแปลงชนิดข้อมูล ไม่ต้องใช้ string method
วิธีเดิม ใช้ substring()
<html> <body> <script>
var str = '2345';
document.write((str.substring(0, str.length - 1)));
</script> </body> </html>
ผลลัพธ์
234
วิธีใหม่ ใช้ Bitwise OR
<html> <body> <script>
document.write(2345 / 10 | 0);
document.write("<br>");
document.write(2345 / 100 | 0);
document.write("<br>");
document.write(2345 / 1000 | 0);
</script> </body> </html>
ผลลัพธ์
234 23 2
หารด้วย 10, 100, 1000 แล้ว Bitwise OR กับ 0 จะตัดทศนิยมได้เลย