Left shift (<<)
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Theleft shift (<<
) operator returns a number or BigInt whose binary representation is the first operand shifted by the specified number of bits to the left. Excess bits shifted off to the left are discarded, and zero bits are shifted in from the right.
Try it
const a = 5; // 00000000000000000000000000000101const b = 2; // 00000000000000000000000000000010console.log(a << b); // 00000000000000000000000000010100// Expected output: 20
Syntax
x << y
Description
The<<
operator is overloaded for two types of operands: number andBigInt. For numbers, the operator returns a 32-bit integer. For BigInts, the operator returns a BigInt. It firstcoerces both operands to numeric values and tests the types of them. It performs BigInt left shift if both operands become BigInts; otherwise, it converts both operands to32-bit integers and performs number left shift. ATypeError
is thrown if one operand becomes a BigInt but the other becomes a number.
The operator operates on the left operand's bit representation intwo's complement. For example,9 << 2
yields 36:
9 (base 10): 00000000000000000000000000001001 (base 2) --------------------------------9 << 2 (base 10): 00000000000000000000000000100100 (base 2) = 36 (base 10)
Bitwise a 32-bit integerx
to the left byy
bits yieldsx * 2 ** y
. So for example,9 << 3
is equivalent to9 * (2 ** 3) = 9 * (8) = 72
.
If the left operand is a number with more than 32 bits, it will get the most significant bits discarded. For example, the following integer with more than 32 bits will be converted to a 32-bit integer:
Before: 11100110111110100000000000000110000000000001After: 10100000000000000110000000000001
The right operand will be converted to an unsigned 32-bit integer and then taken modulo 32, so the actual shift offset will always be a positive integer between 0 and 31, inclusive. For example,100 << 32
is the same as100 << 0
(and produces100
) because 32 modulo 32 is 0.
Warning:You may see people using<< 0
to truncate numbers to integers. Left shifting any numberx
by0
returnsx
converted to a 32-bit integer, which additionally removes leading bits for numbers outside the range -2147483648 to 2147483647. UseMath.trunc()
instead.
For BigInts, there's no truncation. Conceptually, understand positive BigInts as having an infinite number of leading0
bits, and negative BigInts having an infinite number of leading1
bits.
Examples
Using left shift
9 << 3; // 72// 9 * (2 ** 3) = 9 * (8) = 729n << 3n; // 72n
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-left-shift-operator |