Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

มี & Logical Operator ใน JavaScript หรือไม่

คำตอบ

ไม่มี & เดียวคือ bitwise AND operator ใน JavaScript AND ให้ผล 1 เมื่อทั้งสอง input เป็น 1 มิฉะนั้นเป็น 0

ตัวอย่างโค้ด

var firstValue=5; 
var secondValue=4; 
var result=firstValue & secondValue; 
console.log(result);

วิธีรัน

node fileName.js

ชื่อไฟล์ demo172.js

ผลลัพธ์

PS C:\Users\Amit\javascript-code> node demo172.js 
4

ค่า 5 เป็น binary 101 และ 4 เป็น 100

  5 = 1 0 1 
  4 = 1 0 0 
---------------- 
    1 0 0 = 4

คำอธิบาย

&& คือ logical AND สำหรับ boolean logic
& คือ bitwise AND ทำงานระดับ bit ใช้สำหรับ bit manipulation ไม่ใช่ logical comparison