Skip to Content
ScriptingJavaScript Basics
Open in ChatGPTOpen in Claude

JavaScript Basics

JavaScript is a modern alternative to Lua for FiveM scripting. FiveM runs JavaScript separately in client and server contexts. Keep shared data serializable, use runtime-specific natives only on the correct side, and avoid blocking loops so other resource callbacks continue to execute.

Variables

const name = 'John' let age = 25 var isActive = true

Functions

function greet(name) { return `Hello, ${name}` } const message = greet('World')

Objects

const player = { name: 'John', age: 25, items: ['sword', 'shield'], } console.log(player.name) // "John" console.log(player.items[0]) // "sword"

FiveM Functions

// Get the local player's ped (client) const ped = PlayerPedId() // Get ped coords const coords = GetEntityCoords(ped) // Trigger event emit('myevent', { source: 'js-basics' })

Async/Await

Where fetch is available depends on the context. Client scripts have no browser or Node.js APIs (JavaScript runtime manual ). Server scripts default to Node.js 16, which has no global fetch; add node_version '22' to fxmanifest.lua or use PerformHttpRequest. NUI pages run in Chromium and have the browser fetch.

// Server script with node_version '22', or an NUI page async function fetchData() { const response = await fetch('https://api.example.com/data') const data = await response.json() return data }

Common Patterns

Debouncing

let lastTime = 0 function debouncedAction() { const currentTime = GetGameTimer() if (currentTime - lastTime < 1000) { return } lastTime = currentTime // Do action }

Threading

setTick(() => { // Do something every frame })