Updated:

The basic form

/difficulty peaceful
/difficulty easy
/difficulty normal
/difficulty hard

Bedrock also accepts single letters (p, e, n, h) and numbers 0–3. On Java, /difficulty on its own shows the current setting.

What changes

DifficultyMain differences
PeacefulNo hostile mobs (existing ones vanish); hunger does not drop and health regenerates.
EasyWeaker attacks; starving stops at 5 hearts.
NormalStandard; starving stops at half a heart.
HardStronger attacks; starving can kill; zombies can break wooden doors.

Some enemies such as the Ender Dragon stay in Peaceful. Mob drops such as gunpowder and bones become hard to get, so some players switch back only while farming.

Differences worth knowing (Java)

EffectEasyNormalHard
Villager killed by a zombie becomes a zombie villagerNeverHalf the timeAlways
Cave spider poisonNoneAbout 7 sAbout 15 s
Zombies break wooden doorsNoNoYes

If you want to cure zombie villagers, switch to Hard so the conversion never fails.

Local difficulty

Even at the same setting, mobs get tougher the longer players spend in an area and around the full moon. This "local difficulty" raises the chance of mobs carrying armor and enchanted weapons. On Java it is shown as Local Difficulty on the F3 screen.

On a server

An operator runs it in game, or in the console without the slash (difficulty hard). After a restart, the server uses difficulty= from server.properties (default easy), so change that too to keep it.

When it cannot be changed

Good to know

Check your command

The Command Error Doctor checks common syntax mistakes in your browser.