Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The & (Bitwise AND) operator performs a logical AND operation on each pair of corresponding bits of its numeric operands. When applied to standard JavaScript Number values, it evaluates the operands as 32-bit signed integers. When applied to BigInt values, it operates on arbitrary-precision integers without size truncation. It returns a 1 in each bit position where both operands have a 1; otherwise, it returns 0.
operand1 & operand2

Execution Mechanics

When the & operator is invoked, the JavaScript engine evaluates the operands based on their types using the following sequence:
  1. Type Validation and Coercion:
    • If both operands are BigInt values, they bypass 32-bit conversion and are evaluated as arbitrary-precision integers.
    • If the operands are a mix of BigInt and Number, the engine throws a TypeError.
    • If either operand is a Symbol, the engine throws a TypeError.
    • For other types, the engine implicitly coerces the operands to standard Number values. Strings and Booleans are parsed to their numeric equivalents. Values that resolve to NaN (e.g., undefined, unparseable strings) or null ultimately evaluate to 0 during integer conversion.
  2. Bitwise Alignment and Evaluation:
    • For Number operands: The engine applies the internal ToInt32 abstract operation. Fractional components of floating-point numbers are truncated, and the values are converted to 32-bit signed integers in two’s complement format.
    • For BigInt operands: The engine aligns the arbitrary-precision binary sequences. Conceptually, the shorter sequence is sign-extended (padded with its sign bit) to match the length of the longer sequence.
    • The engine evaluates the aligned sequences bit-by-bit using the following truth table: | Bit A | Bit B | Result (A & B) | | :---: | :---: | :---: | | 0 | 0 | 0 | | 0 | 1 | 0 | | 1 | 0 | 0 | | 1 | 1 | 1 |
  3. Return:
    • For Number operands, the resulting 32-bit binary sequence is converted back into a standard JavaScript Number (IEEE 754 double-precision 64-bit float) and returned.
    • For BigInt operands, the resulting binary sequence is returned as a new BigInt.

Syntax and Evaluation Examples

Standard Integer Evaluation
const a = 12; // Binary: 00000000000000000000000000001100
const b = 10; // Binary: 00000000000000000000000000001010

const result = a & b; 
// Result: 8  // Binary: 00000000000000000000000000001000
BigInt Evaluation (Arbitrary-Precision)
// BigInts are not truncated to 32 bits
const bigA = 4294967297n; // Binary: 100000000000000000000000000000001
const bigB = 4294967299n; // Binary: 100000000000000000000000000000011

const result = bigA & bigB;
// Result: 4294967297n
Floating-Point Truncation
// Operands are truncated to 14 and 9 via ToInt32 before the bitwise operation
const result = 14.99 & 9.12; 
// 14 Binary: 00000000000000000000000000001110
//  9 Binary: 00000000000000000000000000001001
// Result: 8  (Binary: ...1000)
Implicit Type Coercion
"7" & 3;     // String "7" coerces to Number 7. Result: 3
true & 5;    // Boolean true coerces to Number 1. Result: 1
NaN & 15;    // NaN coerces to 0 via ToInt32. Result: 0
null & 8;    // null coerces to 0 via ToInt32. Result: 0
Type Errors (Strict Validation)
10n & 5;     // TypeError: Cannot mix BigInt and other types
10 & 5n;     // TypeError: Cannot mix BigInt and other types
Symbol("a") & 1; // TypeError: Cannot convert a Symbol value to a number
Negative Integer Evaluation (Two’s Complement)
const a = -5; // Binary: 11111111111111111111111111111011
const b = 14; // Binary: 00000000000000000000000000001110

const result = a & b;
// Result: 10 // Binary: 00000000000000000000000000001010
Master JavaScript with Deep Grasping Methodology!Learn More