navigator.getGamepads() is the snapshot entry point to the Gamepad API. Learn the returned array (including null slots), gamepadconnected, reading buttons and axes, five examples, and try-it labs.
01
Kind
Method
02
Returns
Gamepad[] (may include null)
03
Status
Baseline Widely available
04
API family
Gamepad API
05
Events
gamepadconnected
06
Input
buttons · axes
Fundamentals
Introduction
Controllers, joysticks, and many gamepads can talk to the browser through the Gamepad API. navigator.getGamepads() returns the current snapshot of connected pads.
It is not a live stream by itself. Games usually listen for connect/disconnect events, then poll getGamepads() inside a requestAnimationFrame loop to read buttons and sticks.
💡
Press a button first
On many browsers the pad list stays empty until the user presses a button on the controller. That is normal — listen for gamepadconnected.
Concept
Understanding the getGamepads() Method
No parameters — call navigator.getGamepads().
Returns an array — each entry is a Gamepad or null.
Stable indexes — null holes keep other pads at the same index after disconnect.
Permissions Policy — may throw SecurityError if blocked.
Foundation
📝 Syntax
General form of the method:
JavaScript
navigator.getGamepads()
Parameters
None.
Return value
An Array of Gamepad objects (may be empty; entries may be null).
Common patterns
JavaScript
// MDN: use connect event, then read by index
window.addEventListener("gamepadconnected", (e) => {
const gp = navigator.getGamepads()[e.gamepad.index];
console.log(
`Gamepad connected at index ${gp.index}: ${gp.id} with ${gp.buttons.length} buttons, ${gp.axes.length} axes.`
);
});
// Skip null slots when listing pads
function listConnectedPads() {
return Array.from(navigator.getGamepads()).filter(Boolean);
}
navigator.getGamepads() is Baseline Widely available across modern browsers (MDN: since March 2017). Controllers still usually need a user button press before they appear in the array. Always skip null slots and poll during gameplay.
✓ Baseline · Widely available
Navigator.getGamepads()
Array snapshot of Gamepad objects — buttons, axes, and stable indexes.
UniversalWidely available
Google ChromeGamepad API · press button to activate
Full support
Mozilla FirefoxGamepad API · modern versions
Full support
Microsoft EdgeGamepad API · Chromium
Full support
Apple SafariGamepad API · modern versions
Full support
OperaGamepad API · modern versions
Full support
Internet ExplorerNo modern Gamepad getGamepads path
Unavailable
getGamepads()Excellent
Bottom line: Listen for gamepadconnected, call getGamepads() for snapshots, filter nulls, and poll buttons/axes each animation frame.
Wrap Up
Conclusion
navigator.getGamepads() is the Baseline way to snapshot connected controllers. Wait for a button press / gamepadconnected, skip null slots, then poll buttons and axes in your game loop.
Poll getGamepads() each animation frame during play
Prompt users to press a button to activate the pad
Handle Permissions Policy SecurityError
❌ Don’t
Assume pads appear without a user gesture
Forget that disconnect can leave null holes
Cache one Gamepad object forever without re-polling
Require a controller when keyboard/mouse works
Ignore deadzones on analog axes
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about getGamepads()
Snapshot controllers — connect event, then poll each frame.
5
Core concepts
🎮01
Returns
Gamepad array
Value
⚡02
Baseline
Widely available
Since 2017
🚫03
Holes
null slots OK
Indexes
🖱04
Activate
press a button
Connect
🎯05
Update
poll each frame
rAF
❓ Frequently Asked Questions
It returns an array of Gamepad objects for controllers connected to the device. Some slots may be null if a pad disconnected, so remaining pads keep the same index.
No. MDN marks it Baseline Widely available (since March 2017). It is not Deprecated, Experimental, or Non-standard — no status banner is required.
Many browsers only expose gamepads after the user presses a button on the controller (privacy / gesture). Listen for gamepadconnected, then call getGamepads().
If a gamepad disconnects mid-session, its slot may become null so other pads keep stable indexes. Always skip null entries when looping.
Use gamepad.buttons (pressed / value) and gamepad.axes (usually -1 to 1). Poll getGamepads() each animation frame for continuous input.
Yes. A SecurityError can be thrown if use is blocked by a Permissions Policy.
Did you know?
A Gamepad object from an earlier poll can go stale. Always call navigator.getGamepads() again (or re-read the same index) when you need fresh button and axis values.